如何分割数组数据从json字符串返回在php


How to split array data returned from json string in php

我有json返回字符串如下所示。我想提取对象的取消政策列表,如截止时间和退款百分比。我试过使用for循环,但它没有帮助我。你能帮我提取这个吗。

Array ( 
[apiStatus] => Array ( [success] => 1 [message] => SUCCESS ) <br>
[apiAvailableBuses] => Array ( <br>
 [0] => Array ( [droppingPoints] => [availableSeats] => 41 <br>[partialCancellationAllowed] => [arrivalTime] => 08:00 AM <br>
  [cancellationPolicy] => [<br>
   {"cutoffTime":"1","refundInPercentage":"10"},<br>
   {"cutoffTime":"2","refundInPercentage":"50"},<br>
   {"cutoffTime":"4","refundInPercentage":"90"}<br>
   ] <br>
   [boardingPoints] => Array ( [0] => Array ( [time] => 09:00PM [location] => Ameerpet,|Jeans Corner 9687452130 [id] => 6 ) [1] => Array ( [time] => 09:15PM [location] => S.R Nagar,S.R Nagar [id] => 2224 ) [2] => Array ( [time] => 09:10PM [location] => Kondapur,Toyota Show room [id] => 2244 ) ) [operatorName] => Deepak Travels [departureTime] => 9:00 PM [mTicketAllowed] => [idProofRequired] => [serviceId] => 6622 [fare] => 800 [busType] => 2+1 Hi-Tech Non A/c [routeScheduleId] => 6622 [commPCT] => 0 [operatorId] => 213 [inventoryType] => 0 ) <br>
 [1] => Array ( [droppingPoints] => [availableSeats] => 41 [partialCancellationAllowed] => [arrivalTime] => 07:00 AM <br>
 [cancellationPolicy] => [<br>
 {"cutoffTime":"1","refundInPercentage":"10"},<br>
 {"cutoffTime":"2","refundInPercentage":"50"},<br>
 {"cutoffTime":"4","refundInPercentage":"90"}<br>
 ] <br>
 [boardingPoints] => Array ( [0] => Array ( [time] => 09:10PM [location] => Ameerpet,|Jeans Corner [id] => 6 ) [1] => Array ( [time] => 09:00PM [location] => S.R Nagar,S.R Nagar [id] => 2224 ) [2] => Array ( [time] => 08:30PM [location] => KUKATPALLY,JNTU [id] => 2230 ) ) [operatorName] => Dhanunjayabus [departureTime] => 9:00 PM [mTicketAllowed] => [idProofRequired] => [serviceId] => 6743 [fare] => 900 [busType] => VOLVO [routeScheduleId] => 6743 [commPCT] => 0 [operatorId] => 233 [inventoryType] => 0 )
 )
)

使用foreach():

foreach ($your_response['apiAvailableBuses'] as $el) {
    $cancellationPolicy[] = $el['cancellationPolicy'];
}

试试这个:

foreach($data['apiStatus']['apiAvailableBuses'] as $item) {
    foreach($item['cancellationPolicy'] as $key => $json) {
        $jsonDecoded = json_decode($json, true);
        // And you will have access to the data like this
        // $jsonDecoded['cutoffTime'];
        // $jsonDecoded['refundInPercentage'];
    }
}
$response = json_decode($apiResponse);
$cancellationPolicies = [];
foreach ($response->apiAvailableBuses as $availableBus) {
    $cancellationPolicies[] = $availableBus['cancellationPolicy'];
    // if you want to display something you can simply do it like this;
    echo $availableBus['cancellationPolicy']->cutoffTime;
}