1

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"
}
paschcua
  • 13
  • 1
  • 3

2 Answers2

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:

$lookup = array();

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

foreach ($arr1 as $element)
{
    if (isset($lookup[$element['image_id']]))
    {
        // output $element['imagepath']
    }
}
MatsLindh
  • 49,529
  • 4
  • 53
  • 84
  • Thanks alot for this solution, the 2nd should be perfect.. Unfortunaly, i have the same problem as i describe in the comments-box above from "Samuels" post/solution :( cant figure it out till yet.. – paschcua Nov 19 '12 at 21:26
  • Got it now with the edited solution above, thanks alot for support! – paschcua Nov 19 '12 at 21:44
1
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.

Samuel Cook
  • 16,620
  • 7
  • 50
  • 62
  • Thanks alot Samuel, for your fast reply! This should be a good solution.. But i forgot to say, that all images should order by the structure of the 2nd array.. the $multidimensional_array2 is in a while loop.. and in the real code (in my project), the 2nd array will also output title and text to each post-"ID".. so now it should always assign the right picture from multi-array1 to the ID of the 2nd array.. do u know a solution for this? thanks alot for your help.. – paschcua Nov 19 '12 at 18:33
  • 1
    you should just be able to echo the title, and whatever other information coming from `$multidimensional_array2` inside of the if statement, just how it is now – Samuel Cook Nov 19 '12 at 19:04
  • Sure, thats not the problem, my problem is another :( my output is just like below in a while loop from 2nd multi-array: $multidimensional_array2['title'], $multidimensional_array2['text'] and last but not least i have the values for $multidimensional_array2['ID']. So all the images with the associated image_id's from 1st multi-array should assigned exactly to the ID's from 2nd multi-array.. for example.. i get 2146 so take image apple.jpg from 1st multi-array, get 2037, so take ananas.jpg etc. (the problem is, the whole 2 multi-arrays are not so "nicely" ordered like above) – paschcua Nov 19 '12 at 21:06