PHP - 如果数组中的字段相等,则比较2个多维数组和输出值

时间:2021-06-26 12:20:46

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").

我在2个变量中有2个数组,它们都在字段中包含完全相同的值(在第1个数组中=“image_id”-field和第2个数组=“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)

我需要比较2个字段并想输出第1个数组的imagepath字符串(如果第1个数组的“ID”字段和第2个数组的字段相等)

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.

这样的事情:如果来自第一个多阵列的“2146”等于来自第二个多阵列的“2146”,那么echo apple.jpg ..但是这怎么工作?它在最后几天真的让我感到非常震惊......感谢您提前回复。

$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"
}

2 个解决方案

#1


2  

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:

如果信息来自同一数据库中的两个表,那么只需将表连接在一起就可以获得更好的效果。如果数组的顺序不相同或不相同(因此$ i可能引用两个数组中的不同元素),请使用一个作为查找表:

$lookup = array();

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

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

#2


2  

foreach($multidimensional_array1 as $arr1){
    foreach($multidimensional_array2 as $arr2){
        if($arr2['id']==$arr1['image_id']){
            echo $arr1['imagepath'];
        }
    }
}

Note: The larger the arrays become the longer this will take.

注意:数组越大,所需的时间越长。

#1


2  

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:

如果信息来自同一数据库中的两个表,那么只需将表连接在一起就可以获得更好的效果。如果数组的顺序不相同或不相同(因此$ i可能引用两个数组中的不同元素),请使用一个作为查找表:

$lookup = array();

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

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

#2


2  

foreach($multidimensional_array1 as $arr1){
    foreach($multidimensional_array2 as $arr2){
        if($arr2['id']==$arr1['image_id']){
            echo $arr1['imagepath'];
        }
    }
}

Note: The larger the arrays become the longer this will take.

注意:数组越大,所需的时间越长。