Last active
September 26, 2020 20:13
-
-
Save jimgwhit/cbbe5bb0d2556fdc7e37a86d3630239c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
On a PHP coding forum someone ask how to loop a certain array. | |
The data was in Json format (not shown), but this will give a good idea of how to handle looping an array. | |
I just named the Json data $mydata. | |
First json decode it: | |
``` | |
$decoded = json_decode($mydata, true); | |
// I just put the json in the variable $mydata to work with. | |
``` | |
The array is this: | |
``` | |
array:3 [▼ | |
"status" => 200 | |
"data" => array:7 [▼ | |
"id" => 5 | |
"salescode" => "sales1" | |
"username" => "Sales1" | |
"role" => "sales" | |
"parent_id" => 10 | |
"created_at" => "2020-07-04 19:57:23" | |
"updated_at" => "2020-07-05 02:13:18" | |
] | |
"role" => array:2 [▼ | |
0 => array:1 [▼ | |
0 => array:5 [▼ | |
"id" => 10 | |
"parent_id" => 11 | |
"code" => "tl" | |
"username" => "admin" | |
"role" => "admin" | |
] | |
] | |
1 => array:2 [▼ | |
0 => array:5 [▼ | |
"id" => 11 | |
"parent_id" => 12 | |
"code" => "sp01" | |
"username" => "Supervisor" | |
"role" => "supervisor" | |
] | |
1 => array:5 [▼ | |
"id" => 26 | |
"parent_id" => 12 | |
"code" => "sp02" | |
"username" => "Supervisor" | |
"role" => "supervisor" | |
] | |
] | |
] | |
] | |
``` | |
They wanted to loop the role section. | |
So the role section would be: | |
``` | |
$decoded["role"] | |
``` | |
Next get the keys for that part of the array: | |
``` | |
$keys = array_keys($decoded["role"]); | |
``` | |
Notice 2 nestings under that, so 2 foreach loops: | |
``` | |
foreach ($decoded["role"] as $key => $value) { | |
if (is_array($value)) { | |
for ($j = 0; $j < count($value); $j++) { | |
foreach ($value[$keys[$j]] as $a => $b) { | |
echo $a . " : " . $b . "<br>"; | |
} | |
echo "-----------------------------------"; | |
echo "<br>"; | |
} | |
} | |
} | |
``` | |
The output: | |
``` | |
id : 10 | |
parent_id : 11 | |
code : tl | |
username : admin | |
role : admin | |
----------------------------------- | |
id : 11 | |
parent_id : 12 | |
code : sp01 | |
username : Supervisor | |
role : supervisor | |
----------------------------------- | |
id : 26 | |
parent_id : 12 | |
code : sp02 | |
username : Supervisor | |
role : supervisor | |
----------------------------------- | |
``` | |
The echo with ------ is just for demo, you would probably use a table or ul li element to display in the view. | |
It doesn't cover all cases, but it should give a good idea of looping an array. | |
And there are other array methods that help with looping, see the PHP manual for more on array techniques. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can also use a flatten function on the section you need, so:
Looping over shows:
Here is flatten function I found in a S.O. post:
But again, look at various array techniques in the PHP manual.