如何拆分从php中的json字符串返回的数组数据

时间:2016-10-24 07:06:47

标签: php arrays json

我有json返回字符串,如下所示。我想提取取消政策列表中的对象,如截止时间和退款百分比。我尝试使用for-loop但它并没有帮助我。你能帮我解决这个问题。

break

3 个答案:

答案 0 :(得分:0)

使用foreach()就像这样:

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

答案 1 :(得分:0)

试试这个:

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'];
    }
}

答案 2 :(得分:0)

$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;
}