0

JSON FEED:

 {
 "Group": [
 {
 "name": "HolderOne",
 "operators": [
 {
 "username": "ken",
 "status": 3
 },
 .....etc.....

CODE:

 <?php 
 $json = file_get_content('path to feed');
 $data=json_decode($json); 
 echo $data->cGroup[0]->operators[0]->username; //WORKS!
 if (is_array($data->Group->operators))
 { foreach($data->Group->operators as $operator) 
 {if($operator->username == "ken") {echo $operator->status;}} 
 } else { echo 'NOT AN ARRAY'; } //DOESNT WORK - DISPLAYS NOT AN ARRAY
 ?>

I am trying to say if the username is Ken (or whatever I specify when I code more) display the correspoding status.

So...

 echo $data->Group[0]->operators[0]->username; //WORKS!

but...

 foreach($data->Group->operators as $operator) {if($operator->username == "ken") {echo $operator->status;}} 

...doesn't - probably something obvious, but can anyone see my problem?

Cheers Andy

asked Oct 26, 2012 at 18:08
0

2 Answers 2

1

It would have to be $data->Group[0]->operators, or loop through the Groups and have another loop inside...

foreach($data->Group as $group){
 foreach($group->operators as $operator){
 if($operator->username == "ken") {echo $operator->status;}
 }
}
answered Oct 26, 2012 at 18:12
Sign up to request clarification or add additional context in comments.

Comments

0

I think it's true:

foreach($data["Group"]["operators"] as $operators)
{
 if($operators["username"] == "ken")
 {
 echo $operators["status"];
 }
 else
 {
 echo 'NOT AN ARRAY';
 }
}
answered Oct 26, 2012 at 19:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.