PHP从json结果中获取变量

PHP从json结果中获取变量,php,json,Php,Json,使用PHP,我得到了一个json结果 {"text_block":[{"text":"XYZ","left":0,"top":0,"width":10,"height":12}]} 我可以通过以下代码将其打印出来: $json= file_get_contents('https://api.url'); $result = json_decode($json, true); //this returns an array $result = json_decode($json); $data

使用PHP,我得到了一个json结果

{"text_block":[{"text":"XYZ","left":0,"top":0,"width":10,"height":12}]}
我可以通过以下代码将其打印出来:

$json= file_get_contents('https://api.url');
$result = json_decode($json, true); //this returns an array
$result = json_decode($json);
$data = get_object_vars(json_decode($json));
$data = array_slice( $data, 0, 10 ); // now you can array functions
echo json_encode( $data );
但需要将文本“XYZ”放入变量中,以便在PHP脚本中进一步使用。我怎么能做到这一点,我已经检查了各种来源,但似乎没有任何进展!谢谢

看起来“XYZ”的值应该是

$data["text_block"][0]["text"]
暂时将最后一行更改为var_dump($data),以便熟悉php数组结构

$res = json_decode($data,true);
$my_text = $res['text_block'][0]['text'];

现在您可以使用my_text作为变量

为什么要对json解码三次?这是在测试吗?数组(1){[“文本块”]=>array(1){[0]=>object(stdClass){[4(5){[“文本”]=>string(7)“XYZ”[“左”]=>int(0)[“顶”]=>int(0)[“宽”=>int(10)[“高”=>int(128)}}}看起来右边,两个嵌套数组中有一个对象。每个对象或数组的每个元素都有一个键,我们需要的第一个键是“text_block”,第二个键是0,第三个键是“text”。你知道这和答案有什么关系吗?