在php中从数组中提取信息

时间:2013-01-23 22:48:58

标签: php arrays parsing web

任何人都可以告诉我如何使用php从下面的数组中提取'honda'值吗?

{
        "version": "1.0",
        "encoding": "UTF-8",
        "entry": {
            "name": "bob",
            "car": {
                "model": "honda"
            }
        }
    }

3 个答案:

答案 0 :(得分:2)

这看起来像一个json编码对象。你能做的是:

$info = json_decode($data, true); //where $data has your stuff from the question
$carModel = $obj['entry']['car']['model'];

答案 1 :(得分:1)

如果你在名为“obj”的变量中拥有所有这些,那么

$obj = '{ "version": "1.0", "encoding": "UTF-8", "entry": { "name": "bob", "car": { "model": "honda" } } }';     
$arr = json_decode($obj, true);
echo $arr['entry']['car']['model'];

应该是'honda'

EDITED:Per Omar下面,你需要将true作为json_decode中的第二个参数。他应该被选为正确的答案。

答案 2 :(得分:0)

使用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);