簡體   English   中英

php:如何從嵌套數組中獲取值

[英]php: how to get a value from a nested array

我有下面使用$a=file_get_contents的 JSON 響應,我需要回顯 [id] 的值,在本例中為 2316713。 當我使用echo $a["id"]我得到這個輸出: "R" 。 有什么建議?

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
)

我可以模擬一個響應並解析它:

<?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);

輸出:

string(3) "bar"

但是,如果您期望使用 JSON,那么您可能需要更改請求,並以不同的方式處理響應。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM