There is an array of this type:

Array ( [0] => Array ( [date] => 07.08.2018 [name] => Название 1 [desc] => Описание 1. ) [1] => Array ( [date] => 07.08.2018 [name] => Название 2 [desc] => Описание 2. ) ) 

Those. In an array of arrays in the three keys.

How to delete arrays in which one or more keys are not filled? Those. leave only those fields in which everything is filled?

For example, in such situations:

 Array ( [0] => Array ( [date] => 07.08.2018 [name] => Название 1 [desc] => Описание 1. ) [1] => Array ( [date] => 07.08.2018 [name] => Описание 2 [desc] => Описание 2. ) [2] => Array ( [date] => [name] => [desc] => ) ) 

or

 Array ( [0] => Array ( [date] => 07.08.2018 [name] => Название 1 [desc] => Описание 1. ) [1] => Array ( [date] => 07.08.2018 [name] => Название 2 [desc] => Описание 2. ) [2] => Array ( [date] => [name] => Описание 3 [desc] => ) ) 

Delete array with key [2] .

    2 answers 2

     $arr = [ [ 'date' => '07.08.2018', 'name' => 'Название 1', 'desc' => 'Описание 1.' ], [ 'date' => '07.08.2018', 'name' => 'Название 1', 'desc' => 'Описание 1.' ], [ 'date' => '', 'name' => 'Название 1', 'desc' => '.' ] ]; foreach ($arr as $idx => $subarray) { foreach ($subarray as $name => $item) { if ($item == null) unset($arr[$idx]); } } print_r($arr); 
    • Thank you, great). I understand that in the main question this was not, but still, how can you check what is entered? Those. that the date string is really a date, and the rest is just text (cleaned from tags)? If something is wrong in one of the arrays, then, for example, break the loop and issue die() ? - Aaron
    • @Aaron, that is, to make a check on the date format and on the emptiness of the elements? - Let's say Pie
    • Yes, if empty, then just delete empty arrays. If the error is filled, then do not delete, but return the error. What you need to check: the date format (DD.MM.YYYY) and text fields (just clean it from the tags html, php, js, etc.). Text fields are easy to clean and move on. if the date is incorrect, stop the cycle with an error. Something like that - Aaron
    • @Aaron, like that? - Let's say Pie
    • Yes, an interesting option. Cleaning the fields, apparently, should be done on the same principle. And you can not wrap a separate function check? - Aaron
     <?php $array = array( 0 => array( "date" => "07.08.2018", "name" => "Название 1", "desc" => "Описание 1" ), 1 => array( "date" => "07.08.2018", "name" => "Название 2", "desc" => "Описание 2" ), 2 => array( "date" => "", "name" => "Название 3", "desc" => "" ) ); $array = array_filter($array, function($arr) { if (count(array_filter($arr)) == count($arr)) return $arr; }); print_r($array); ?> 
    • Have you seen what happens if you have a different number of elements in the sub-array, not always equal to 3? - Let's say Pie
    • @ Let'ssayPie is in general, this is a special case, that is, three elements each. But since it will be so easy for everyone, corrected the code. - greg zakharov