0

I have created an array and as an example, using my sample array, I want to search for the [entity_id] element and retrieve the [value] element associated with it. For my example array if I searched for entity_id 4 it would return a 1. How do I do this with PHP?

Array
(
[0] => Array
    (
        [entity_id] => 1
        [value] => 0
    )

[1] => Array
    (
        [entity_id] => 2
        [value] => 0
    )

[2] => Array
    (
        [entity_id] => 3
        [value] => 2
    )

[3] => Array
    (
        [entity_id] => 4
        [value] => 1
    )

)
0

1 Answer 1

1
echo $array[array_search('4', array_column($array, 'entity_id'))]['value'];

get the outer key by extracting the entity_id column and searching that, then use that key to access the inner value

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.