0

I´m learning to operate with JSON response by using PHP. And I would like to know how to select just specific data arrays depends on values from the other data field. So far I have this progress:

$result = json_decode(do_post_request("https://domain/my_api_key/", $postdata, true));` 

echo '<pre>';

print_r($result);

gives me:

      stdClass Object ( [response] => stdClass Object ( [count] => 991 [msisdn] => Array ( [0] => stdClass Object ( [msisdn] => 420607659770 [GsmSubscription] => isActive 
[waiting_for_response] => [imsi] => 230031030037243 [iccid] => 8942031013792372436 ) [1] => stdClass Object ( [msisdn] => 420731037691 [GsmSubscription] => isActive 
[waiting_for_response] => [imsi] => 230031030037242 [iccid] => 8942031013792372428 ) [2] => stdClass Object ( [msisdn] => 420732763471 [GsmSubscription] => isActive 
[waiting_for_response] => [imsi] => 230031030037241 [iccid] => 8942031013792372410 ) [3] => stdClass Object ( [msisdn] => 420732788951 [GsmSubscription] => isActive 
[waiting_for_response] => [imsi] => 230031030037244 [iccid] => 8942031013792372444 ) [4] => stdClass Object ( [msisdn] => 420735041563 [GsmSubscription] => isActive 
[waiting_for_response] => [imsi] => 230031030037200 [iccid] => 8942031013792372006 ) [5] => stdClass Object ( [msisdn] => 420778890012 [GsmSubscription] => isActive 
[waiting_for_response] => [imsi] => 230031030010134 [iccid] => 8942031013392101342 ) [6] => stdClass Object ( [msisdn] => 420778890078 [GsmSubscription] => Suspend 
[waiting_for_response] => [imsi] => 230031030010244 [iccid] => 8942031013392102449 ) [7] => stdClass Object ( [msisdn] => 420778899001 [GsmSubscription] => Preactivate 
[waiting_for_response] => [imsi] => 230031030037210 [iccid] => 8942031013792372105 ) [8] => stdClass Object ( [msisdn] => 420778899002 [GsmSubscription] => Suspend 
[waiting_for_response] => [imsi] => 230031030037400 [iccid] => 8942031013792374002 ) [9] => stdClass Object ( [msisdn] => 420778899003 [GsmSubscription] => isActive 
[waiting_for_response] => [imsi] => 230031030037401 [iccid] => 8942031013792374010 ) [10] => stdClass Object ( [msisdn] => 420778899004 [GsmSubscription] => Suspend 
[waiting_for_response] => [imsi] => 230031030037402 [iccid] => 8942031013792374028 ) [11] => stdClass Object ( [msisdn] => 420778899005 [GsmSubscription] => Suspend 
[waiting_for_response] => [imsi] => 230031030037403 [iccid] => 8942031013792374036 ) [12] => stdClass Object ( [msisdn] => 420778899006 [GsmSubscription] => Suspend 
[waiting_for_response] => [imsi] => 230031030037404 [iccid] => 8942031013792374044 ) [13] => stdClass Object ( [msisdn] => 420778899007 [GsmSubscription] => isActive 
[waiting_for_response] => [imsi] => 230031030037405 [iccid] => 8942031013792374051 ) [14] => stdClass Object ( [msisdn] => 420778899008 [GsmSubscription] => Suspend 
[waiting_for_response] => [imsi] => 230031030037406 [iccid] => 8942031013792374069 ) [15] => stdClass Object ( [msisdn] => 420778899009 [GsmSubscription] => isActive 
[waiting_for_response] => [imsi] => 230031030037407 [iccid] => 8942031013792374077 ) [16] => stdClass Object ( [msisdn] => 420778899010 [GsmSubscription] => Suspend 
[waiting_for_response] => [imsi] => 230031030037408 [iccid] => 8942031013792374085 ) [17] => stdClass Object ( [msisdn] => 420778899011 [GsmSubscription] => Preactivate 
[waiting_for_response] => [imsi] => 230031030037409 [iccid] => 8942031013792374093 ) [18] => stdClass Object ( [msisdn] => 420778899012 [GsmSubscription] => Preactivate [waiting_for_response] => [imsi] => 230031030037410 [iccid] => 8942031013792374101 ) [19] => stdClass Object ( [msisdn] => 420778899013 [GsmSubscription] => isActive 
[waiting_for_response] => [imsi] => 230031030037411 [iccid] => 8942031013792374119 ) ) ) [error] => ) 

print_r gives me an array of full JSON reponse, but I need to select only MSISDN number and display it in HTML table. I can handle it by:

echo "<table>";
foreach($result->response->msisdn as $value)
{
$msisdnnumbers = $value->msisdn;
echo  "<tr><td>" . $msisdnnumbers . "</td></tr>";
}

echo "</table>";

This gives me all MSISDN fileds and that is what I want. Except I would like to echo just the MSISDN fileds where the value of [GsmSubscription] = "" (empty-no value) ???

Can I echo just those specific data? And how by PHP?

4
  • You need to manually construct your HTML and echo it. Commented Apr 28, 2015 at 10:00
  • So I use "echo" instead "print_r", but what is the right syntax in PHP: echo $result->msisdn; echo $result["msisdn"]; I found those two examples. I have stil the problem to see the value of "msisdn". I do not know if my value json_decode($reponse, true); is OK. The true parameter is important,right? Becasue for decoding I am using: $result = json_decode(do_post_request("my_api_key", $postdata, true)); Still cannot echo this number. Commented Apr 28, 2015 at 10:42
  • checkout my edited answer. Commented Apr 28, 2015 at 21:02
  • I checked it, you really gives me a good direction. I am very closed now, but I need to select just msisdn numbers where I can compare with other data field called GsmSubscription. Can you give me a small advice, so I can move forward. Thank you. Commented Apr 29, 2015 at 11:42

4 Answers 4

1

To access an object member you use $obj->KEY; You could use foreach to access number of object. In your case, try this code

foreach($result->response->msisdn as $value)
 {
 echo $value->msisdn;
 }
Sign up to request clarification or add additional context in comments.

3 Comments

what format does this code display. echo '<pre>'; print_r($result); can you please update and edit into your question.
Hi Sam, I edited my question. Sorry it is not so clear but I am a newbie in JSON and PHP.
This is perfect solution.
0

Print_r is used to print an array. Echo is used to print a single array value. So if you do:

echo $result["msisdn"];

you will print the msisdn value.

Comments

0

print_r is used to print array.

Use echo to print the msisdn value:

echo $result["msisdn"];

So, should work correctly.

1 Comment

When I use echo that way, it gives me an error, blank website page .. I have to use the format $result->msisdn; something like that, there´s no way to use the brackets.
0

Please, look closely to the answer. The thing you maybe wants is :

echo $result["response"]["msisdn"];

Here is the script to beautify PHP objects : http://phillihp.com/toolz/php-array-beautifier/

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.