Skip to content

Instantly share code, notes, and snippets.

@jimgwhit

jimgwhit/Array_loop_md

Last active Sep 26, 2020
Embed
What would you like to do?
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.
@jimgwhit

This comment has been minimized.

Copy link
Owner Author

@jimgwhit jimgwhit commented Sep 26, 2020

You can also use a flatten function on the section you need, so:

        $decoded = json_decode($mydata, true);
        $s = $this->array_flatten($decoded["role"]);
        $keys = array_keys($s);
        for ($i = 0; $i < count($s); $i++) {       //         <---  To loop over each sub array
            foreach ($s[$keys[$i]] as $key => $value) {
                echo $key . " : " . $value . "<br>";
            }
            echo "-----------------------------------";
            echo "<br>";
        }
        

Looping over shows:

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
-----------------------------------

Here is flatten function I found in a S.O. post:

    public function array_flatten($a, $flat = []) {
        $entry = [];
        foreach ($a as $key => $el) {
            if (is_array($el)) {
                $flat = $this->array_flatten($el, $flat);
            } else {
                $entry[$key] = $el;
            }
        }
        if (!empty($entry)) {
            $flat[] = $entry;
        }
        return $flat;
    }

But again, look at various array techniques in the PHP manual.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment