我正在学习使用PHP操作JSON响应。我想知道如何选择特定的数据数组取决于来自其他数据字段的值。到目前为止,我有这个进步:
$result = json_decode(do_post_request("https://domain/my_api_key/", $postdata, true));`
echo '<pre>';
print_r($result);
给了我:
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给了我一个完整的JSON响应数组,但是我只需要选择MSISDN号并在HTML表中显示它。我可以通过以下方式处理:
echo "<table>";
foreach($result->response->msisdn as $value)
{
$msisdnnumbers = $value->msisdn;
echo "<tr><td>" . $msisdnnumbers . "</td></tr>";
}
echo "</table>";
这给了我所有的MSISDN文件,这就是我想要的。除了我想回复MSISDN文件,其中[GsmSubscription] =“”的值(空 - 无值)???
我能回复那些特定数据吗?以及如何通过PHP?
答案 0 :(得分:1)
要访问对象成员,请使用$ obj-&gt; KEY;您可以使用foreach来访问对象的数量。在您的情况下,请尝试此代码
foreach($result->response->msisdn as $value)
{
echo $value->msisdn;
}
答案 1 :(得分:0)
Print_r用于打印数组。 Echo用于打印单个数组值。所以如果你这样做:
echo $result["msisdn"];
你将打印msisdn值。
答案 2 :(得分:0)
print_r用于打印数组。
使用echo打印msisdn值:
echo $result["msisdn"];
所以,应该正常工作。
答案 3 :(得分:0)
请仔细看看答案。你可能想要的是:
echo $result["response"]["msisdn"];
以下是美化PHP对象的脚本:http://phillihp.com/toolz/php-array-beautifier/