我有这个简单的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']);
但是这给了我所有颜色的列表而不是键/值
答案 0 :(得分:1)
$ii = 0;
$resultArr = array();
while (isset($jsonArr['colour' . $ii])) {
$resultArr[] = array('colour' => $jsonArr['colour' . $ii]);
$ii++;
}