从Array - JSON条件打印特定的JSON数据

时间:2015-05-04 11:55:28

标签: php json

我正在调用API函数来获取我的提供者的活动电话号码列表,我可以按如下方式打印JSON响应:

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] => 420778897001 [GsmSubscription] => Aging [waiting_for_response] => [imsi] => [iccid] => ) 
[8] => stdClass Object ( [msisdn] => 420778897002 [GsmSubscription] => [waiting_for_response] => [imsi] => [iccid] => ) 
[9] => stdClass Object ( [msisdn] => 420778897003 [GsmSubscription] => [waiting_for_response] => [imsi] => [iccid] => ) 
[10] => stdClass Object ( [msisdn] => 420778897004 [GsmSubscription] => [waiting_for_response] => [imsi] => [iccid] => ) 
[11] => stdClass Object ( [msisdn] => 420778897005 [GsmSubscription] => [waiting_for_response] => [imsi] => [iccid] => ) 
[12] => stdClass Object ( [msisdn] => 420778897006 [GsmSubscription] => [waiting_for_response] => [imsi] => [iccid] => ) 
[13] => stdClass Object ( [msisdn] => 420778897007 [GsmSubscription] => [waiting_for_response] => [imsi] => [iccid] => ) 
[14] => stdClass Object ( [msisdn] => 420778897008 [GsmSubscription] => [waiting_for_response] => [imsi] => [iccid] => ) 
[15] => stdClass Object ( [msisdn] => 420778897009 [GsmSubscription] => [waiting_for_response] => [imsi] => [iccid] => ) 
[16] => stdClass Object ( [msisdn] => 420778897010 [GsmSubscription] => [waiting_for_response] => [imsi] => [iccid] => ) 
[17] => stdClass Object ( [msisdn] => 420778897011 [GsmSubscription] => [waiting_for_response] => [imsi] => [iccid] => ) 
[18] => stdClass Object ( [msisdn] => 420778897012 [GsmSubscription] => [waiting_for_response] => [imsi] => [iccid] => ) 
[19] => stdClass Object ( [msisdn] => 420778897013 [GsmSubscription] => [waiting_for_response] => [imsi] => [iccid] => ) ) ) [error] => )

所以,如果这是完整的响应,那么我可以选择我需要的特定数据,在这种情况下是“msisdn”数字:

foreach($result->response->msisdn as $value)
 {
$msisdnnumbers = $value->msisdn;

通过这个PHP回声,我有来自JSON响应的“msisdn”数字列表。

但是现在,我想补充一些条件。例如,我想只回显“msmdn”数字,其中“GsmSubscription”字段包含值“Suspend”。 PHP可以添加这个条件吗?

1 个答案:

答案 0 :(得分:0)

如果我没错,那么你必须使用if条件

foreach($result->response->msisdn as $value)
 {
  if($value->GsmSubscription === 'Suspend'){
    $msisdnnumbers[] = $value->msisdn;// to get an array of values
  } else {
    continue;
  }
 }