简体   繁体   中英

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM