数组迭代php

时间:2012-12-27 14:09:57

标签: php arrays json

我是php的新手,我有一些问题要打印一些JSON内容

这是我的代码

//Read text file
$json_data = file_get_contents('data/data.txt');

//Decode it
$obj=json_decode($json_data, true);

//Print array content
print_r($obj);

//Loop the array to write content
foreach( $obj as $Item ){

    // add comment to html list
    echo $Item;
}

我的JSON文件

[{"name":"ken"}, {"name":"barbie"}]

我的输出

(
    [0] => Array ( [name] => ken )
    [1] => Array ( [name] => barbie )
)
ArrayArray

如何只打印“ken”和“barbie”?

谢谢你!

1 个答案:

答案 0 :(得分:6)

更改

echo $Item;

echo $Item['name'];
相关问题