0

With the following, I am pushing values into $countries = array();

array_push($countries, 
    array(
        'state'=> $state,
        'date'=> $jikuDate
    )
);

Then I need to do a check and I am doing:

if (in_array($name, $countries['state'])) {

How to check the value if its inside a multi level associative array?

Kevin
  • 41,694
  • 12
  • 53
  • 70
rob.m
  • 9,843
  • 19
  • 73
  • 162
  • you can use array_map() function – Davit Huroyan Jun 04 '18 at 06:32
  • You have no choice but some kind of iteration over the array. Probably the easiest solution is something like `in_array($name, array_map(function($c) { return $c['state']; }, $countries)` – whites11 Jun 04 '18 at 06:33
  • Possible duplicate of [PHP multidimensional array search by value](https://stackoverflow.com/questions/6661530/php-multidimensional-array-search-by-value) – Nigel Ren Jun 04 '18 at 06:36
  • Although in the duplicate, I think the accepted answer is a bit out of date. – Nigel Ren Jun 04 '18 at 06:37

2 Answers2

1

You can't use in_array outright since it'll only accept a flat array. Just use array_column to get all state values, then make your checking.

Here's the idea:

if (in_array($name, array_column($countries, 'state'))) {

}

Basically, if your $countries are multi leveled, (meaning):

Array
(
    [0] => Array
        (
            [state] => Arizona
            [date] => 2018-01-01
        )

    [1] => Array
        (
            [state] => California
            [date] => 2018-01-01
        )

)

When you apply array_column, you sorta pluck out each state values, then gather them inside an array. It will yield an array like this:

array_column($countries, 'state') ===> Array ( [0] => Arizona [1] => California )

Only then you can use in_array. Just check out the manual, its eloquently stated there very well.

Kevin
  • 41,694
  • 12
  • 53
  • 70
  • yes works, just one last question if I might, trying to loop `['state'] ` the manual says `foreach ($countries as $cou => $value) { `but what is $value? – rob.m Jun 04 '18 at 06:52
  • @rob.m you mean `$value` in `foreach ($countries as $cou => $value) {` ?, it contains each array inside `$countries`, if you mean `$countries['state']`, it should show an undefined offset since its not present there – Kevin Jun 04 '18 at 06:58
  • exactly, how would we loop only on `$countries['state']` using that `foreach`? – rob.m Jun 04 '18 at 06:59
  • 1
    @rob.m if you want to use a `foreach` to work on it, just use `foreach ($countries as $value) { echo $value['state']; // use this }`, it will correspond to the current iteration state value, you can try it if you want – Kevin Jun 04 '18 at 07:01
1

I use this code. The has function checks whether a given item or items exists in an array using "dot" notation.

i hope usefull for you :

function exists($array, $key)
{
    if ($array instanceof ArrayAccess) {
        return $array->offsetExists($key);
    }

    return array_key_exists($key, $array);
} 


function accessible($value)
{
    return is_array($value) || $value instanceof ArrayAccess;
}

 function has($array, $keys)
{
    if (is_null($keys)) {
        return false;
}

$keys = (array) $keys;

if (! $array) {
    return false;
}

if ($keys === []) {
    return false;
}

foreach ($keys as $key) {
    $subKeyArray = $array;

    if (exists($array, $key)) {
        continue;
    }

    foreach (explode('.', $key) as $segment) {
        if (accessible($subKeyArray) && exists($subKeyArray, $segment)) {
            $subKeyArray = $subKeyArray[$segment];
        } else {
            return false;
        }
    }
}

return true;
}

for example :

$array = ['product' => ['name' => 'Desk', 'price' => 100]];
var_dump(has($array,'ali')); # return false
var_dump(has($array,'product')); # return true
var_dump(has($array,'product.name')); # retirm true
Alihossein shahabi
  • 4,034
  • 2
  • 33
  • 53