0

Hi I have two uneven JSON array which I am decoding in php. First array has 2 values and other has 3 values now I want to search for an 'id' number from first array to second array and display the names. Is there any way to do it? I would appreciate your help. Thank you

Here is the example of my first array

[
{
    "id": 5,
    "pb_first_name": "Respect",
    "pb_last_name": "Respect"
},
{
    "id": 41,
    "pb_first_name": "Master",
    "pb_last_name": "Master"
}

]

Here is the second JSON array

[
{
    "id": 5,
    "type": "Suite",
    "description": "",
    "number": "105",
    "floor": 1
},
{
   "id": 23,
    "type": "Suite",
    "description": "",
    "number": "220",
    "floor": 2
},
{
   "id": 41,
    "type": "Penthouse",
    "description": "",
    "number": "410",
    "floor": 4
}

]

3
  • 2
    Question isn't exactly clear. What are you trying to get from the arrays? Could to elaborate? Commented Nov 26, 2012 at 15:35
  • json is irrelevant. if you've decoded them, then they're PHP arrays. there's no such thing as a "json array". there's just json strings, which ENCODE arrays/objects/values. Commented Nov 26, 2012 at 15:36
  • K i have updated my question. I have to display names with same ids. Commented Nov 26, 2012 at 15:52

1 Answer 1

2

You can use array_diff, array_intersect, in_array or array_search. Since you haven't mentioned any code, I don't know which one would best suit your need.

Manuals: array_diff, array_intersect, in_array, array_search

Edit:

$arr1 = array(...); // 1st array
$arr2 = array(...); //2nd array
foreach($arr2 as $v) {
  foreach($arr1 as $m) {
   if ($v['id'] == $m['id'])
    echo $m[pb_first_name'] . " " . $m['pb_last_name'];
 }
}
Sign up to request clarification or add additional context in comments.

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.