简体   繁体   中英

php: how to get a value from a nested array

I have the JSON response below using $a=file_get_contents , and I need to echo the value of [id] which is 2316713 in this example. When I use echo $a["id"] I get this output: "R" . Any suggestions?

Response:
---------
Array
(
    [id] => 2316713
    [key] => xxxxxx
    [status] => initialising
    [sandbox] => 1
    [created_at] => 2018-02-26T23:38:52Z
    [finished_at] => 
    [source_file] => Array
        (
            [id] => 29757828
            [name] => test1.pptx
            [size] => 134718
        )
    [target_files] => Array
        (
        )
    [target_format] => png
    [credit_cost] => 1
)

I can mock a response and parse that:

<?php

$data =
[
    'foo' => 'bar',
    'cakes' => 'hot'
];

$response =
"Response:\n
---------\n" . print_r($data, true);

$foo = preg_match('/\[foo\] => (.*)/', $response, $match)
    ? $match[1]
    : null;

var_dump($foo);

Output:

string(3) "bar"

But if you expect JSON, perhaps you need to change your request, and handle the response differently.

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