如果id存在,PHP json_decode()将获取父数组

时间:2017-08-07 14:25:27

标签: php arrays multidimensional-array

好的,所以我使用此代码获取JSON响应:

$response = curl_exec($curl);
$json = json_decode($response, true);
    echo '<pre>';
    print_r($json);
    echo '</pre>';

这是我得到的结果:

Array
(
    [has_more] => 1
    [data] => Array
        (
            [0] => Array
                (
                    [ticketNumber] => 172164226315700075
                    [iceValue] => 0
                    [approvalCode] => 000000
                    [subtotalIVA0] => 0
                    [binCard] => 411111
                    [requestAmount] => 8
                    [id] => 193172164226315739
                )

            [1] => Array
                (
                    [ticketNumber] => 172164210625700073
                    [iceValue] => 0
                    [approvalCode] => 000000
                    [subtotalIVA0] => 0
                    [Metadata] => Array
                        (
                            [id_suscription] => 0000000000004245
                        )

                    [binCard] => 411111
                    [requestAmount] => 80
                    [id] => 193172164210625732
                )

        )

)

我需要检查每个数组是否存在[id_suscription],如果等于if([id_suscription] == '0000000000004245')之类的值来获取父数组,这是我期望得到的结果:

Array
(
    [ticketNumber] => 172164210625700073
    [iceValue] => 0
    [approvalCode] => 000000
    [subtotalIVA0] => 0
    [Metadata] => Array
        (
            [id_suscription] => 0000000000004245
        )

    [binCard] => 411111
    [requestAmount] => 80
    [id] => 193172164210625732
)

2 个答案:

答案 0 :(得分:1)

嗯,但你的问题在哪里?!

我认为这可能对你有所帮助。

$items = [
    'has_more' => 1,

    'data' =>  
    [
        [
            'ticketNumber' => 172164226315700075,
            'iceValue' => 0,
            'approvalCode' => 000000,
            'subtotalIVA0' => 0,
            'binCard' => 411111,
            'requestAmount' => 8,
            'id' => 193172164226315739,                 
            'Metadata' => 
            [
                'id_suscription' => 0000000000004244
            ]
        ],
        [
            'ticketNumber' => 172164226315700075,
            'iceValue' => 0,
            'approvalCode' => 000000,
            'subtotalIVA0' => 0,
            'binCard' => 411111,
            'requestAmount' => 8,
            'id' => 193172164226315739,                 
            'Metadata' => 
            [
                'id_suscription' => 0000000000004245
            ]
        ]
    ]
];

function getItem($items){       
    foreach($items['data'] as $item){
        if(isset($item['Metadata'])){
            if(isset($item['Metadata']['id_suscription']) && $item['Metadata']['id_suscription'] == 0000000000004245){
                return $item;
            }
        }
    }
}

print_r(getItem($items));

答案 1 :(得分:1)

也许你需要这样的东西:

$result = [];
if(isset($response['data']) && count($response['data'])>0) {
    foreach ($response['data'] as $value) {
        if(isset($value['Metadata']) && isset($value['Metadata']['id_suscription']) && $value['Metadata']['id_suscription'] == '0000000000004245') {
            $result = $value;
            break;
        }
    }
}
print_r($result);