谁能告诉我如何使用 php 从下面的数组中提取“honda”值?
{
"version": "1.0",
"encoding": "UTF-8",
"entry": {
"name": "bob",
"car": {
"model": "honda"
}
}
}
这看起来像一个 json 编码的对象。你可以做的是:
$info = json_decode($data, true); //where $data has your stuff from the question
$carModel = $obj['entry']['car']['model'];
如果你在一个名为“obj”的变量中拥有所有这些,那么
$obj = '{ "version": "1.0", "encoding": "UTF-8", "entry": { "name": "bob", "car": { "model": "honda" } } }';
$arr = json_decode($obj, true);
echo $arr['entry']['car']['model'];
应该是“本田”
已编辑:根据下面的 Omar,您确实需要 true 作为 json_decode 中的第二个参数。他应该被选为正确答案。
使用 json_decode 为:http ://php.net/manual/fr/function.json-decode.php
<?php
$json = '{"version": "1.0","encoding": "UTF-8","entry": {"name": "bob","car": {"model": "honda"} } }';
$tab = json_decode($json, true);
$honda = $tab['entry']['car']['model'];
var_dump($honda);
// or with object:
$obj = json_decode($json);
$honda = $obj->entry->car->model;
var_dump($honda);