我想将数组转换为JSON格式,该怎么做。 下面是我的代码
$arr['more_images'][] = $datas;
echo '<pre>';
print_r($arr['more_images']);
echo '</pre>';
In above code i am getting output like this
<pre>Array
(
[0] => Array
(
[0] => http://sale4allz.com/sale4allz/public/uploads/product_more_images/178_3749.jpg
[1] => http://sale4allz.com/sale4allz/public/uploads/product_more_images/178_4923.jpg
[2] => http://sale4allz.com/sale4allz/public/uploads/product_more_images/178_9362.jpg
)
)
</pre>
But I want output like this
[
{
http://sale4allz.com/sale4allz/public/uploads/product_more_images/178_3749.jpg
}
{ http://sale4allz.com/sale4allz/public/uploads/product_more_images/178_4923.jpg
}
{ http://sale4allz.com/sale4allz/public/uploads/product_more_images/178_9362.jpg
}
]
我该怎么做?如果有人知道解决方案,请告诉我们
答案 0 :(得分:1)
您应使用以下逻辑:
header('Content-Type: application/json');
echo json_encode( $arr['more_images']);
答案 1 :(得分:0)
您应该尝试以下操作:
$arr['more_images'][] = json_encode($datas);
echo '<pre>';
print_r($arr['more_images']);
echo '</pre>';
OR
$arr['more_images'][] = $datas;
echo '<pre>';
print_r(json_encode($arr['more_images']));
echo '</pre>';