Omzy Posted June 4, 2010 Share Posted June 4, 2010 Here is my multi-dimensional array: $subcats=array( 'lighting-effects'=>array( 'light-shows'=>array('Light Shows', 'Wedding Light Shows'), 'sound-systems'=>array('Sound Systems', 'Wedding Sound Systems'), 'visual-displays'=>array('Visual Displays', 'Wedding Visual Displays'), 'fireworks-pyrotechnics'=>array('Fireworks/Pyrotechnics', 'Wedding Fireworks/Pyrotechnics'), 'star-cloths'=>array('Star Cloths', 'Wedding Star Cloths'), 'mist-generators'=>array('Mist Generators', 'Wedding Mist Generators'), 'virtual-flames'=>array('Virtual Flames', 'Wedding Virtual Flames'), 'confetti-canons'=>array('Confetti Cannons', 'Wedding Confetti Cannons'), ), ); I want to run a foreach loop on the 'lighting-effects' array keys (light-shows, sound-systems, etc). Currently I do it like this: foreach($subcats as $index1 => $value1) { foreach($value1 as $index2 => $value2) { // processing here } } But I'm wondering if this is a long winded way of doing it/is there a quicker way? Quote Link to comment https://forums.phpfreaks.com/topic/203902-foreach-loop-on-multi-dimensional-array/ Share on other sites More sharing options...
dabaR Posted June 4, 2010 Share Posted June 4, 2010 Does foreach ($subcats['lighting-effects'] as $category => $effects)... do the trick? I think you would benefit from learning how to better name your variables, so you could read http://www.objectmentor.com/resources/articles/naming.htm Quote Link to comment https://forums.phpfreaks.com/topic/203902-foreach-loop-on-multi-dimensional-array/#findComment-1067931 Share on other sites More sharing options...
Psycho Posted June 4, 2010 Share Posted June 4, 2010 That is perfectly reasonable, although I would give the variables more logical names. dabaR's method would save a single step (save a nanosecond or two) but would give the code a more compact/logical display. However, it would not work if there are multiple main categories. Quote Link to comment https://forums.phpfreaks.com/topic/203902-foreach-loop-on-multi-dimensional-array/#findComment-1067936 Share on other sites More sharing options...
Omzy Posted June 4, 2010 Author Share Posted June 4, 2010 Hi there and thanks! Yes I should have mentioned, there are multiple main categories! I just wanted to reduce the code and have only one loop to be honest. Quote Link to comment https://forums.phpfreaks.com/topic/203902-foreach-loop-on-multi-dimensional-array/#findComment-1067959 Share on other sites More sharing options...
dabaR Posted June 4, 2010 Share Posted June 4, 2010 AFAIK, you have to have as many loops as levels of arrays. Quote Link to comment https://forums.phpfreaks.com/topic/203902-foreach-loop-on-multi-dimensional-array/#findComment-1067961 Share on other sites More sharing options...
Psycho Posted June 4, 2010 Share Posted June 4, 2010 Well, there isn't going to be a "quicker" method, but depending on your needs you can create a function to return a single array with all the child values: function allSubcategoryValues($multiArray) { $outputArray = array(); foreach ($multiArray as $subCat) { $outputArray = array_merge($outputArray, $subCat); } return $outputArray; } foreach(allSubcategoryValues($subcats) as $value) { // processing here } Quote Link to comment https://forums.phpfreaks.com/topic/203902-foreach-loop-on-multi-dimensional-array/#findComment-1067975 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.