0

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?

1
  • Are you going to pass ['item_number'] in the call to API? Commented Sep 8, 2017 at 12:50

1 Answer 1

2

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This is just what I needed!

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.