Reputation: 4323
I have this section of PHP code, which takes the response from an API and then checks to see if a particular part of the response matches a variable.
Like below... in the array attribution
is there a source_id
that matches $this_id
?
$this_id = 15;
$array = json_decode($response, true);
if (in_array($this_id, array_column($array['data']['attribution'], 'source_id'))) {
// then do this
}
else {
// then do this instead
}
This works perfectly. What I'm trying to do is get some additional data from this array without having to call the API all over again. The logic would be as above, but then if true
return the value for ['data']['attribution']['item_number']
. Keeping in mind that there might be a bunch of objects in the attribution
.
How can I return this value?
Upvotes: 0
Views: 62
Reputation: 40673
change the in_array
to array_search
:
$this_id = 15;
$array = json_decode($response, true);
$ind = array_search($this_id,array_column($array['data']['attribution'], 'source_id'));
if ($ind !== false) {
$found = $array['data']['attribution'][$ind];
//then do this and also use $found
}else{
//then do this instead
}
Upvotes: 2