jarvis Posted October 14, 2018 Share Posted October 14, 2018 Hi, How do you prevent the following happening: Array ( [] => Array So, effectively I want to remove that one from the main array. i.e. Array ( [] => Array ( [0] => Japanese GP [1] => Japanese GP [2] => Williams ) [2005] => Array ( [0] => Japanese GP [1] => Williams ) [2006] => Array ( [0] => Japanese GP ) ) Becomes: Array ( [2005] => Array ( [0] => Japanese GP [1] => Williams ) [2006] => Array ( [0] => Japanese GP ) ) I tried using: if (!empty($year['value'])): Within my foreach but sadly that didn't work Thanks Link to comment Share on other sites More sharing options...
Barand Posted October 14, 2018 Share Posted October 14, 2018 use array_filter() with the ARRAY_FILTER_USE_KEY flag set. EDIT: Of course the easiest method is not to put data into the array that you then want to remove PS If you prefer to use a foreach loop then $result = []; foreach ($array as $k => $v) { if ($k) { $result[$k] = $v; } } Link to comment Share on other sites More sharing options...
jarvis Posted October 14, 2018 Author Share Posted October 14, 2018 Thanks again! Link to comment Share on other sites More sharing options...
Barand Posted October 14, 2018 Share Posted October 14, 2018 23 minutes ago, Barand said: use array_filter() with the ARRAY_FILTER_USE_KEY flag set. For anyone who's interested, the code would be $result = array_filter($array, function($v) {return $v;}, ARRAY_FILTER_USE_KEY); Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.