hostfreak Posted November 21, 2007 Share Posted November 21, 2007 I've got an array that produces the following: Array ( [username] => Array ( [0] => Username must be entered [1] => Username must be at least 6 characters ) [Password] => Array ( [0] => Password must be entered ) ) What would be the best way to sort through it. I know a method (see below), but would like opinions on what people think is the way to go. Method I am currently using: foreach ($this->errors as $errors) //$this->errors is the array { while (list($k, $v) = each ($errors)) { echo $v . '<br />'; } } To me it seems a little makeshift seeing as I don't actually need to list the key. Quote Link to comment https://forums.phpfreaks.com/topic/78191-solved-sorting-through-multidimensional-array/ Share on other sites More sharing options...
Psycho Posted November 21, 2007 Share Posted November 21, 2007 I'd use a 2nd foreach loop: foreach ($this->errors as $errors) //$this->errors is the array { foreach ($errors as $error) { echo $error . '<br />'; } } Quote Link to comment https://forums.phpfreaks.com/topic/78191-solved-sorting-through-multidimensional-array/#findComment-395723 Share on other sites More sharing options...
hostfreak Posted November 21, 2007 Author Share Posted November 21, 2007 Not sure why that didn't cross my mind. Anyways works great and looks a lot better to me. Thanks mjdamato. Quote Link to comment https://forums.phpfreaks.com/topic/78191-solved-sorting-through-multidimensional-array/#findComment-396045 Share on other sites More sharing options...
hostfreak Posted November 23, 2007 Author Share Posted November 23, 2007 Sorry to bring this topic back up, however there is another question I would like to ask. I want to be able to group the errors under what multidimensional array they fall under. So I would need to extract the name of the array, for example: [username] => Array //Only want to e I would need to extract "Username". All suggestions are appreciated, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/78191-solved-sorting-through-multidimensional-array/#findComment-397356 Share on other sites More sharing options...
Psycho Posted November 23, 2007 Share Posted November 23, 2007 <?php foreach ($this->errors as $error_cat => $error_ary) //$this->errors is the array { echo "<b>$error_cat</b><br />\n"; foreach ($error_ary as $error) { echo "$error<br />\n";; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/78191-solved-sorting-through-multidimensional-array/#findComment-397744 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.