Issue
I have an array, $row2.
In $row2 two arrays are present. The output of $row2 is:
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] =>
)
)
I want to remove null elements from the array, but I can't do this.
I tried the following methods:
array_filter($row2);
array_filter($row2, function($var){return !is_null($var);});
array_diff($rows2, array("null", ""));
Solution
I've an one liner solution to filter out null
values from multidimensional array if you use array_map along with 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>';
Output:
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
)
)
DEMO : https://eval.in/975240
Answered By - Always Sunny
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.