我有一个数组,$row2。
在$row2中存在两个数组。 $row2 的输出是:
Array
(
[0] => Array
(
[Proposal_id] => 9
[row] => 1
[col1] => 2
[col2] => 2
[col3] =>
[col4] =>
[col5] =>
[Type] => customtbl
[Invoice_term] =>
[Qoute] =>
[Rate_per_hour] =>
[Total] =>
)
[1] => Array
(
[Proposal_id] => 9
[row] => 2
[col1] => 3
[col2] => 4
[col3] =>
[col4] =>
[col5] =>
[Type] => customtbl
[Invoice_term] =>
[Qoute] =>
[Rate_per_hour] =>
[Total] =>
)
)
我想从数组中删除空元素,但我做不到。
我尝试了以下方法:
array_filter($row2);
array_filter($row2, function($var){return !is_null($var);});
array_diff($rows2, array("null", ""));
最佳答案
如果您使用array_map,我有一个单行解决方案可以从多维数组中过滤掉null
值。以及array_filter ,
$array = [
['Proposal_id' => 9,
'row' => 1,
'col1' => 2,
'col2' => 2,
'col3' => null,
'col4' => null,
'col5' => null,
'Type' => 'customtbl',
'Invoice_term' => null,
'Qoute' => null,
'Rate_per_hour' => null,
'Total' => null,
],
[
'Proposal_id' => 9,
'row' => 1,
'col1' => 2,
'col2' => 2 ,
'col3' => null,
'col4' => null,
'col5' => null,
'Type' => 'customtbl',
'Invoice_term' => null,
'Qoute' => null,
'Rate_per_hour' => null,
'Total' => null,
]
];
$af = array_filter(array_map('array_filter', $array));
print '<pre>';
print_r($af);
print '</pre>';
输出:
Array
(
[0] => Array
(
[Proposal_id] => 9
[row] => 1
[col1] => 2
[col2] => 2
[Type] => customtbl
)
[1] => Array
(
[Proposal_id] => 9
[row] => 1
[col1] => 2
[col2] => 2
[Type] => customtbl
)
)
关于php - 无法从嵌套数组中删除空元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49390322/