PHP - 比较2多维数组和输出值如果阵列平等领域 [英] PHP - Compare 2 multidimensional arrays and output values if equal fields in array

查看:107
本文介绍了PHP - 比较2多维数组和输出值如果阵列平等领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个变量2阵列和两个含有它们的确切字段中的值相同(在第1个数组=image_id已场内和第2个数组=ID字段中)。

我需要比较的2场,想输出第一阵列的ImagePath字符串(如果第1个数组的ID已场内和第二阵列的场均相等)

事情是这样的:
如果2146,从第1多阵列等于2146,从第2多阵列,然后回声apple.jpg ..
但如何运作的?它真的刻着我出了最后的日子..在此先感谢您的答复。

$ multidimensional_array1:

 阵列(4){
  [0] =>
  串(9)apple.jpg
  [的ImagePath] =>
  串(9)apple.jpg
  [1] =>
  串(4)2146
  [image_id] =>
  串(4)2146
}阵列(4){
  [0] =>
  串(10)ananas.jpg
  [的ImagePath] =>
  串(10)ananas.jpg
  [1] =>
  串(4)2037
  [image_id] =>
  串(4)2037
}阵列(4){
  [0] =>
  串(8)nuts.jpg
  [的ImagePath] =>
  串(8)nuts.jpg
  [1] =>
  串(4)2024
  [image_id] =>
  串(4)2024
}

$ multidimensional_array2:

 阵列(2){
  [0] =>
  串(4)2146
  [ID] =>
  串(4)2146
}阵列(2){
  [0] =>
  串(4)2037
  [ID] =>
  串(4)2037
}阵列(2){
  [0] =>
  串(4)2024
  [ID] =>
  串(4)2024
}


解决方案

只要阵列按键相同,长度和顺序,可以遍历之一,并从两个挑选值。

  $ LEN =计数($ ARR1);为($ I = 0; $ I< $ len个; $ I ++)
{
    如果($ ARR1 [$ i] ['image_id'] == $ ARR2 [$ i] ['身份证'])
    {
        //输出$ ARR1 [$ i] ['的ImagePath']
    }
}

如果信息是从同一个数据库的两个表,你将是刚刚加盟的表一起更好。如果阵列未订购的相同或不相同长度的(以便$我可能引用两个阵列不同的元素),可以使用一个作为一个查找表

  $查询=阵列();的foreach($ ARR2为$元素)
{
    $查找[$元素['身份证'] = $元素;
}的foreach($ ARR1为$元素)
{
    如果(使用isset($查找[$元件['image_id']]))
    {
        //输出$元素['的ImagePath']
    }
}

I have 2 Arrays in 2 variables and both of them containing exactly the same values in the fields (in the 1st array = "image_id"-field and in the 2nd array = "ID-field").

I need to compare the 2 fields and would like to output the imagepath string of the 1st array (if the "ID"-field of 1st array and the field of 2nd array are equal)

Something like this: if "2146" from 1st multi-array is equal to "2146" from 2nd multi-array, then echo apple.jpg.. But how does that work? Its really freakin me out the last days.. thanks in advance for your replies.

$multidimensional_array1:

array(4) {
  [0]=>
  string(9) "apple.jpg"
  ["imagepath"]=>
  string(9) "apple.jpg"
  [1]=>
  string(4) "2146"
  ["image_id"]=>
  string(4) "2146"
}

array(4) {
  [0]=>
  string(10) "ananas.jpg"
  ["imagepath"]=>
  string(10) "ananas.jpg"
  [1]=>
  string(4) "2037"
  ["image_id"]=>
  string(4) "2037"
}

array(4) {
  [0]=>
  string(8) "nuts.jpg"
  ["imagepath"]=>
  string(8) "nuts.jpg"
  [1]=>
  string(4) "2024"
  ["image_id"]=>
  string(4) "2024"
}

$multidimensional_array2:

array(2) {
  [0]=>
  string(4) "2146"
  ["ID"]=>
  string(4) "2146"
}

array(2) {
  [0]=>
  string(4) "2037"
  ["ID"]=>
  string(4) "2037"
}

array(2) {
  [0]=>
  string(4) "2024"
  ["ID"]=>
  string(4) "2024"
}

解决方案

As long as the arrays have the same keys, length and order, you can iterate over one and and pick values from both.

$len = count($arr1);

for ($i = 0; $i < $len; $i++)
{
    if ($arr1[$i]['image_id'] == $arr2[$i]['ID'])
    {
        // output $arr1[$i]['imagepath']
    }
}

If the information is from two tables in the same database, you would be better off by just joining the tables together. If the arrays are not ordered the same or not of the same length (so that $i might reference different elements in both arrays), use one as a lookup table:

$lookup = array();

foreach ($arr2 as $element)
{
    $lookup[$element['ID']] = $element;
}

foreach ($arr1 as $element)
{
    if (isset($lookup[$element['image_id']]))
    {
        // output $element['imagepath']
    }
}

这篇关于PHP - 比较2多维数组和输出值如果阵列平等领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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