4

I have the following array:

Array
(
    [0] => Array
        (
            [country_id] => 1
        )

    [1] => Array
        (
            [country_id] => 2
        )

    [2] => Array
        (
            [country_id] => 3
        )

)

I want to basically check if a value is in this array. So if country_id = 1, then it's true, etc.

any help would be awesome!

2 Answers 2

6
$found = false;
foreach ($your_array as $key => $element) {
   if (isset($element['country_id']) && ($element['country_id'] == 1)) {
       $found = $key;
       break;
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Might be an issue when $key == 0.
Only if you do if (!$found). Doing if ($found !== FALSE) will handle that.
-1

There isn't a single magic function in PHP that will make this a simple solution, you could use something like array_map to accomplish this but you will probably be better off just iterating through the entire array and store the entries that match your criteria.

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.