Reputation: 57
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?
Upvotes: 0
Views: 200
Reputation: 347
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;
}
Upvotes: 1
Reputation: 1975
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/
Upvotes: 0
Reputation: 56
print_r is used to print array.
Use echo to print the msisdn value:
echo $result["msisdn"];
So, should work correctly.
Upvotes: 0
Reputation: 6673
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.
Upvotes: 0