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 Quote Link to comment https://forums.phpfreaks.com/topic/307783-prevent-blanks-in-array/ Share on other sites More sharing options...
Barand Posted October 14, 2018 Share Posted October 14, 2018 (edited) 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; } } Edited October 14, 2018 by Barand 1 1 Quote Link to comment https://forums.phpfreaks.com/topic/307783-prevent-blanks-in-array/#findComment-1561569 Share on other sites More sharing options...
jarvis Posted October 14, 2018 Author Share Posted October 14, 2018 Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/307783-prevent-blanks-in-array/#findComment-1561570 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); Quote Link to comment https://forums.phpfreaks.com/topic/307783-prevent-blanks-in-array/#findComment-1561571 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.