Thomas96 Thomas96 -4 years ago 173
PHP Question

array_filter multidimensial array

In my Laravel site I have this array:

array:2 [▼
0 => array:3 [▼
"title" => "asdf"
"desc" => ""
"date" => ""
]
1 => array:3 [▼
"title" => ""
"desc" => ""
"date" => ""
]
]


I want to filter out the empty arrays in my array.
Simply doing

$array = array_filter(request()->exp);


does nothing...

People suggested:

$array = array_filter(array_map('array_filter', request()->exp));


but this results in:

array:1 [▼
0 => array:1 [▼
"title" => "asdf"
]
]


I need those other values even if theyre empty, or my next page wont work.

How do I get around this?

Eventually this has to come out:

array:2 [▼
0 => array:3 [▼
"title" => "asdf"
"desc" => ""
"date" => ""
]


it just simply has to remove empty arrays in the one top array.

Answer Source

As I understand your question, this may help:

$newArr = array();
foreach($arr as $key=>$val){
    $val1 = array_filter($val);
    if(!empty($val1)){
        $newArr[$key] = $val; // or $newArr[] = $val; if don't want to keep index
    }
}
var_dump($newArr);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download