简体   繁体   中英

Print specific JSON data from Array - JSON conditions

I´am calling the API function to get list of active telephone numbers from my provider and I can print the JSON response as follow:

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] => )

So if this is the complete response, than I can select specific data I need, which is the "msisdn" number in this case:

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

By this PHP echo, I have the list of "msisdn" numbers from JSON reponse.

But now, I would like to add some conditions. For example, I would like to echo only the "msisdn" numbers where the field of "GsmSubscription" contain the value "Suspend". Is it possible with PHP to add this condition?

If I'm not wrong then you have to use if condition

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM