简体   繁体   中英

Check Associative 2d Array

I have the following declaration of array

Array
(
    [name] => 1
    [callrate] => 1
    [maxcalls] => 100000
    [mintime] => 5
    [maxtime] => 16
    [skillexps] => Array
        (
            [0] => 1
            [1] => 2
        )
)

How can I check the the array contains the array. I tried the

function is_multi($a) {
    foreach ($a as $v) {
      if (is_array($v)) 
      {
        return "has array";
        break;
      }
      break;
    }
    return 'only value';
}

But this only gives 'only value'. I need to check the If Associative Array is two dimensional.

Why your existing code doesn't work?

Because you're looking every element of array to check is it an array or not using foreach() and is_array() ? if not then break; so when it checks for name element the value is not an array, it is just a integer so it immediately break; and goes out of the foreach() loop and returns only value but when I removed the extra break; from your code it works fine because then it checks for each and every element of your array to verify that is it contains an array or not . I've also added a good looking way how to check array is multi-dimensional or not. Hope this helps :)

function is_multi($a) {
    foreach ($a as $v) {
      if (is_array($v)) 
      {
        return "has array";
        break;
      }
      // removed extra break; from here
    }
    return 'only value';
}

To check array is multi-dimensional or not? I'll do this way ,

<?php
function is_multi(array $array) {
    return count($array) !== count($array, COUNT_RECURSIVE);
}
$array = array
    (
    'name' => 1,
    'callrate' => 1,
    'maxcalls' => 100000,
    'mintime' => 5,
    'maxtime' => 16,
    'skillexps' => array
    (
        1,
        2
    )
);
echo is_multi($array);
?>

DEMO: https://3v4l.org/LANsh

If you are trying to check whether the array is 2d or not , you can use array filter like below

$data=array
(
    'name' => 1,
    'callrate' => 1,
    'maxcalls' => 100000,
    'mintime' => 5,
    'maxtime' => 16,
    'skillexps' =>array(1,2)
);
$filteredItems = array_filter($data, function($elem) {
    return is_array($elem);
});
if(count($filteredItems)>0){
 echo "multi dimensional";
}

else{
 echo "1 dimensional";
}

If you are interested to check every keys,you can use array_map with closure function like below

$data=array
(
    'name' => 1,
    'callrate' => 1,
    'maxcalls' => 100000,
    'mintime' => 5,
    'maxtime' => 16,
    'skillexps' => array
        (
            0 => 1,
            1 => 2
        )
);

$array = array_map(function ($a) {
            return is_array($a) ? "has array" : "only value";
        }, $data);
print_r($array);

output

Array
(
    [name] => only value
    [callrate] => only value
    [maxcalls] => only value
    [mintime] => only value
    [maxtime] => only value
    [skillexps] => has array
)

If you want to check entire array is multidimensional or not then just add one line below

$multi=array_search('has array', $array) ? "is  multi " : "is not multi";
echo $multi;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM