1

Well this has been a headache.

I have two arrays;

$array_1 = Array
(
    [0] => Array
        (
            [id] => 1
            [name] => 'john'
            [age] => 30
        )

[1] => Array
        (
            [id] => 2
            [name] => 'Amma'
            [age] => 28
        )

[2] => Array
        (
            [id] => 3
            [name] => 'Francis'
            [age] => 29
        )

)

And another array

array_2 = = Array
    (
        [0] => Array
            (
                [id] => 2
                [name] => 'Amma'
            )

    )

How can I tell that the id and name of $array_2 are the same as the id and name of $array_1[1] and return $array_1[1]['age']?

Thanks

2
  • Can array_2 have many values in it? Commented Jan 28, 2014 at 18:28
  • 3
    This looks like some sql data, maybe a join would be a good option? Commented Jan 28, 2014 at 18:28

5 Answers 5

3
foreach($array_1 as $id=>$arr)
{
    if($arr["id"]==$array_2[0]["id"] AND $arr["name"]==$array_2[0]["name"])
    {
        //Do your stuff here
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Never thought of that before, Thats a-lot simpler. That'll help me, thanks!
dont you think its exactly the same as my answer ? ;)
You are awesome. Sorry for the delay as I was in a traffic ;)
3

Well you can do it in a straightforward loop. I am going to write a function that takes the FIRST element in $array_2 that matches something in $array_1 and returns the 'age':

function getField($array_1, $array_2, $field)
{
   foreach ($array_2 as $a2) {
      foreach ($array_1 as $a1) {
         $match = true;
         foreach ($a2 as $k => $v) {
            if (!isset($a1[$k]) || $a1[$k] != $a2[$k]) {
              $match = false;
              break;
            }
         }
         if ($match) {
            return $a1[$field];
         }
      }
   }
   return null;
}

2 Comments

Bad idea to use or, it has a lower precedence than !=. php.net/manual/en/language.operators.precedence.php. Means you have to use ||. Or wait, do you mean it? ;)
Hey, I just wonder, did you actually read that?!? Haha, I made a mistake, || or or both have a lower precedence than !=.
2

Use array_diff().

In my opinion, using array_diff() is a more generic solution than simply comparing the specific keys.

Array_diff() returns a new array that represents all entries that exists in the first array and DO NOT exist in the second array.

Since your first array contains 3 keys and the seconds array contains 2 keys, when there's 2 matches, array_diff() will return an array containing the extra key (age).

foreach ($array_1 as $arr) {
   if (count(array_diff($arr, $array_2[1])) === 1) {//meaning 2 out of 3 were a match
      echo $arr['age'];//prints the age
    }
}

Hope this helps!

Comments

1

I assume you want to find the age of somebody that has a known id and name.

This will work :

foreach ($array_1 as $val){
    if($val['id']==$array_2[0]['id'] && $val['name']==$array_1[0]['name']){
        $age = $val['age'];
    }
}
echo $age;

2 Comments

I wonder too. i didn't downvote. But your answer was correct.
If you don't trust me, i can downvote you, to show that your answer will be downvoted with "-1", (Only for testing of course) and then upvote again. Stackoverflow, should really consider showing who downvotes..
0

Try looking into this.

http://www.w3schools.com/php/func_array_diff.asp

And

comparing two arrays in php

-Best

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.