我正在尝试用PHP编写一个函数,但是作为一个新手,我发现这样做有点困难。我有一个看起来像
[{"x":"12345","y":"john"},{"x":"12345","y":"stars"}]
我正在编写的函数是
function getCSV($x)
{
// Now I want to pass the $x which in the above array is 12345 and get "john,stars" as output
}
PHP 中是否有任何可用的方法可以做到这一点,或者获得它的最佳方法是什么?
这
对我来说看起来像一个json。
[{"x":"12345","y":"john"},{"uid1":"12345","uid2":"stars"}]
function getCSV($x)
{
$arr = json_decode($x);
echo $arr[0]->y . ', ' . $arr[1]->uid2;
}
这看起来很可怕,但没有进一步的解释是唯一有效的方法
编辑 - 编辑后
function getCSV($x)
{
$arr = json_decode($x);
$y = array();
foreach($arr as $obj){
$y[] = $obj->y;
}
return implode(',', $y);
}
这是一个工作垫 http://codepad.org/HzttdmjW