PHP從最後一個數組值刪除逗號
$meta = get_post_custom($post->ID);
$images = $meta['img1'][0]; // urls separated with comma
$images = explode(',', $images);
foreach ($images as $image) {
echo '{image : '.$image.', title : "Credit: x"},';
};
輸出:
{image : 'http://localhost/slideshow/1.jpg', title : 'Credit: x'},
{image : 'http://localhost/slideshow/2.jpg', title : 'Credit: x'},
{image : 'http://localhost/slideshow/3.jpg', title : 'Credit: x'},
{image : 'http://localhost/slideshow/4.jpg', title : 'Credit: x'}, // last comma, just after }
我想刪除最後一個評論逗號中輸出。
這正是我試圖得到:
所需的輸出:
{image : 'http://localhost/slideshow/1.jpg', title : 'Credit: x'},
{image : 'http://localhost/slideshow/2.jpg', title : 'Credit: x'},
{image : 'http://localhost/slideshow/3.jpg', title : 'Credit: x'},
{image : 'http://localhost/slideshow/4.jpg', title : 'Credit: x'}
收益如何在PHP函數使用構建生成有效的JSON? (json_encode) –