PHP比较二维数组 [英] PHP compare two dimension array

查看:238
本文介绍了PHP比较二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何比较两个二维数组值。

第一阵列

 阵列1

    [0] =>排列
        (
            [0] =>一个
        )    [1] =>排列
        (
            [0] => b
        )    [2] =>排列
        (
            [0] => C
        )}

第二个

 阵列2

    [0] =>排列
        (
            [0] =>一个
        )    [1] =>排列
        (
            [0] => ð
        )    [2] =>排列
        (
            [0] => Ë
        )}

我需要让我的循环比较阵列和检查匹配的值。就我而言,数组1 [0] [0] =一个比赛ARRAY2 [0] [0] =一个。如果匹配,PHP将输出一些HTML。

我的f​​oreach循环

 的foreach($数组1为$数组){
    的foreach($数组作为$数组){
      //这里需要比较数组2不知道该怎么做。
    }
}


解决方案

 的foreach($数组1为$ K1 => $阵列){
    的foreach($数组作为$ K2 => $ VAL){      如果($数组2 [$ K1] [$ K2] == $ VAL){
          // $ ARRAY1 [$ K1] [$ K2]等于$数组2 [$ K1] [$ K2]
      }
}

的foreach($ A为$ K => $ V)语法做同样的事情为的foreach($ A为$ V) ,但它也让与值相关联的钥匙插入 $氏/ code>。

I would like to know how to compare two two-dimension arrays value.

First array

Array 1
(
    [0] => Array
        (
            [0] => a
        )

    [1] => Array
        (
            [0] => b
        )

    [2] => Array
        (
            [0] => c
        )

}

Second one

Array 2
(
    [0] => Array
        (
            [0] => a
        )

    [1] => Array
        (
            [0] => d
        )

    [2] => Array
        (
            [0] => e
        )

}

I need to make my loop to compare the arrays and check the matched value. In my case, array1[0][0]=a matches array2[0][0]=a. If it matches, php will output some html.

My foreach loop

foreach ($array1 as $arrays){
    foreach($arrays as $array){
      //need to compare array2 here not sure how to do it.
    }
}

解决方案

foreach($array1 as $k1 => $arrays) {
    foreach($arrays as $k2 => $val) {

      if($array2[$k1][$k2] == $val) {
          // $array1[$k1][$k2] is equal to $array2[$k1][$k2]
      }
}

The foreach($a as $k => $v) syntax does the same thing as foreach($a as $v), except that it also puts the key associated with the value into $k.

这篇关于PHP比较二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆