doubi1928 2018-07-05 09:48
浏览 96
已采纳

Php将多维数组中的多个值与另一个多维数组进行比较

I have a multidimensional array

Simple example array

[0]=>array(2) {
["slug"]=>string(12) "exampleslug1"
["tax"]=>string(11) "exampletax1"}
[1]=>array(2) {
["slug"]=>string(12) "exampleslug2"
["tax"]=>string(11) "exampletax2"}
[2]=>array(2) {
["slug"]=>string(12) "exampleslug2"
["tax"]=>string(11) "exampletax3"}

Now i want to check another multidimensional array for both values.

So for example. I want to check if the slug is "exampleslug2" and the tax = "exampletax2" So in this case it should return false/true/false.

Ive tried in_array but i can't seem to get that to work for multidimensional. Can anyone help me out with a solution for checking multiple values in the multidimensional array to be the same as in a second multidimensional array.

If it try something like

if (!in_array($termSlug, $externalTermSlugs['slug']) && !in_array($termTax, $externalTermSlugs['tax']) ) {}

It adds everything multiple times, what i want to have is that it adds everything once unique and doesn't overwrite if it already exists.

  • 写回答

1条回答 默认 最新

  • dougao7801 2018-07-05 09:57
    关注

    try this code

    <?php
    $arr = array( 0 => array("slug"=>"exampleslug1","tax"=>"exampletax1"),
                 1 => array("slug"=>"exampleslug2","tax"=>"exampletax2"),
                 2 => array("slug"=>"exampleslug2","tax"=>"exampletax3"));
    foreach($arr as $key => $value){
        if($value['slug']=="exampleslug2" && $value['tax']=="exampletax2"){
            echo "true"."<br>";
        }else{
            echo "false"."<br>";
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢