PHP:帮助重组JSON数组

时间:2014-01-15 08:56:06

标签: php json

我有这个简单的json对象:

$colours = '{"num":20,"status":"ok","colour0":"red","colour1":"green","colour2":"blue"}';

如何使用PHP变成这个:

$colours = '[{"colour":"red"},{"colour":"green"},{"colour":"blue"}]';

我需要首先使用json_decode()吗?

我试了这个没有运气:

$jsonArr = json_decode($similarsites, true);
$c1 = parse_url($jsonArr['c1']);

$ii = 0;
$resulti = array('color' => array());

while (isset($jsonArr['c' . $ii])) {
        $c = $jsonArr['c' . $ii];

        $resulti['color'][$ii] = $c;
        $ii++ ;
    }

print json_encode($resulti['color']);

但是这给了我所有颜色的列表而不是键/值

1 个答案:

答案 0 :(得分:1)

$ii = 0;
$resultArr = array();
while (isset($jsonArr['colour' . $ii])) {
    $resultArr[] = array('colour' => $jsonArr['colour' . $ii]);
    $ii++;
}

DEMO