1

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
1
  • 1
    What you have try so far? Commented Oct 17, 2016 at 4:57

4 Answers 4

3

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
...
...
...
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.