Home >Backend Development >PHP Tutorial >The default value of a two-dimensional array is empty. How to determine if it is empty and not execute the code inside the if?

The default value of a two-dimensional array is empty. How to determine if it is empty and not execute the code inside the if?

WBOY
WBOYOriginal
2016-07-06 13:51:131299browse

<code>$pet = array(["name"]=>array('','','')["age"]=>array('','',''));

if($pet){
    echo $pet
}

具体代码如上,当数组有值时,才输出。 本人用if 判断失败。</code>

Reply content:

<code>$pet = array(["name"]=>array('','','')["age"]=>array('','',''));

if($pet){
    echo $pet
}

具体代码如上,当数组有值时,才输出。 本人用if 判断失败。</code>

<code>$pet = array("name"=>array('','',''),"age"=>array('','',''));

foreach($pet as $pKey => $pVal){

    $pet[$pKey] = array_filter($pVal);
}
$pet = array_filter($pet);

if(!empty($pet)){
    var_dump($pet);
}
</code>

Is this the main point of the question?

1, code error,

<code>name和age是二维数组的key,之间要逗号隔开。

$pet = array(["name"]=>array('','',''),["age"]=>array('','',''));

if($pet){
    echo $pet
}
</code>

2. When you say "the array is output only when it has a value", does it mean $pet, or name or age.

<code>array_filter过滤一下空数组。
</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn