Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中更改数组的动态键的值_Php_Arrays - Fatal编程技术网

在php中更改数组的动态键的值

在php中更改数组的动态键的值,php,arrays,Php,Arrays,我有一个由动态键组成的动态数组。我想检查这些键是否有空值,然后将其更改为0。有什么好主意吗?谢谢 样本阵列 array ( 'empid' => string 'UPW_OB_8' (length=8) 'TotalLoginHrs' => string '0' (length=1) 'status' => string 'id not used' (length=11) '20140401' => 1 '

我有一个由动态键组成的动态数组。我想检查这些键是否有空值,然后将其更改为0。有什么好主意吗?谢谢 样本阵列

   array (
      'empid' => string 'UPW_OB_8' (length=8)
      'TotalLoginHrs' => string '0' (length=1)
      'status' => string 'id not used' (length=11)
      '20140401' => 1
      '20140402' => null
      '20140403' => 2
      '20140404' => null
      '20140405' => 1
      '20140406' => null
      '20140407' => 9
      '20140408' => null
      '20140409' => 3
      '20140410' => null
      '20140411' => 5
      '20140412' => null
)

您需要检查它并指定0值

foreach ($array as $key => $value) {
    if (is_null($value)) {
         $array[$key] = 0;
    }
}


您可以使用
preg\u replace
查找空值:

$replaced = preg_replace("/^$/u", "0", $array);
示例:

试试这个

$array = array_map(function($value){
    return (is_null($value)) ? "" : $value;
},$array)
$array = array_map(function($value){
    return (is_null($value)) ? "" : $value;
},$array)