|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Looping Through Multi-Dimensional Arrays Using Foreach
In PHP, can you loop through a multi-dimensional array using the “foreach” function, but only make it output key/value pairs that match a specific value?
|
|
#2
|
|||
|
|||
|
foreach will only loop though the elements of the selected array
if you want to display all the dimentions of an array use the function print_r($array); if you only want to print out certain parts of an array, you could use this function PHP Code:
that will do the same as print_r(); however you can then limit what is printed by placing some conditions where i have the line echo "$key: $value<br>"; so you could change that to if($key == $name) { echo "$key: $value<br>"; } so now it will only print the array element when key == name |
|
#3
|
|||
|
|||
|
typo
An oldie but goodie. Helpful stuff, thanks.
Just a couple typos tho. Missing closing parens and semi-colon. It should read: ... if(is_array($value)) { // the value of the current array is also a array, so call this function again to process that array PrintArray($value); } ... |
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > Looping Through Multi-Dimensional Arrays Using Foreach |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|