Van Zark
Van Zark

Reputation: 25

Run a php foreach loop on multi-dimensional array

I need to run a foreach script on a multi-dimensional array.

The original JSON is formatted like:

{
    "_links":{

    },
    "chatter_count":15,
    "chatters":{
        "moderators":[
            "moderator1",
            "moderator2",
            "moderator3"
        ],
        "staff":[
            "staff1",
            "staff2",
            "staff3"
        ],
        "admins":[
            "admin1",
            "admin2",
            "admin3"
        ],
        "global_mods":[
            "global_mod1",
            "global_mod2",
            "global_mod3"
        ],
        "viewers":[
            "viewer1",
            "viewer2",
            "viewer3"
        ]
    }
}

Having run json_decode to get a PHP data structure, I'm now lost on how to run a foreach loop to output something like:

chatter_count: 15 

moderators:
moderator1
moderator2
moderator3

staff:
staff1
staff2
staff3

admins:
admin1
admin2
admin3

global_mods:
global_mod1
global_mod2
global_mod3

viewers:
viewer1
viewer2
viewer3

Upvotes: 1

Views: 499

Answers (4)

Andrew Li
Andrew Li

Reputation: 1055

Try this code.
And you have to add some style for print.

$testobj = json_decode(file_get_contents("https://tmi.twitch.tv/group/user/sodapoppin/chatters"));
print("chatter_count:", $testobj->chatter_count);

foreach ( $testobj->chatters as $key=>$chatter )  
{  
print $key;  
foreach($chatter as $values){  
printf("%s\n", $values);  
 }  

}

Hoping this helps you.

Upvotes: 0

Sofiene Djebali
Sofiene Djebali

Reputation: 4508

PHP

$data = json_decode($json, true);
echo "chatter_count: " . $data["chatter_count"] . "\n";
foreach($data['chatters'] as $chattersK=> $chatters) {
  echo $chattersK . ":\n";
  foreach($chatters as $chatterK => $chatters) {
     echo $chatters . "\n";
  }
  echo "\n";
}

Demo: Eval.in

Upvotes: 0

Mahfuzul Alam
Mahfuzul Alam

Reputation: 3157

You are just few step behind what you're trying to accomplish. To get chatter_count use $testobj ->chatter_count. And then loop through your chatters array use foreach($testobj->chatters as $key => $value) { // write your logic here }. This way you can get what you're trying to accomplish. I can paste the code here, but I would like you to give a try first. Hope you get your hints now.

Upvotes: 0

Thamilhan
Thamilhan

Reputation: 13323

First decode the json to array, then make use of $key to print the array:

<?php

$testobj = json_decode(file_get_contents('https://tmi.twitch.tv/group/user/sodapoppin/chatters'), true);

echo "chatter_count:".$testobj['chatter_count']."\n";

foreach($testobj['chatters'] as $key => $chatter){
    echo "\n$key:\n";
    foreach ($chatter as $value) {
        echo "$value\n";
    }
}

Output:

I get something like this from the URL you gave:

chatter_count:5461

moderators:
emilydk
fyzicul
hnl
hnlbot
ngmack
nixi93
psychostatik
sodapoppin
staystrong420
sxyhxy
tastyphone

staff:
evoli
pluto
...
...
...

Upvotes: 3

Related Questions