我需要創建一個JSON數組中,索引1創建JSON數組索引開始1
這裏是我的代碼從網站上挑選圖片
$image_urls = array();
//get all images URLs in the content
foreach($get_content->find('img') as $element)
{
/* check image URL is valid and name isn't blank.gif/blank.png etc..
you can also use other methods to check if image really exist */
if(!preg_match('/blank.(.*)/i', $element->src) && filter_var($element->src, FILTER_VALIDATE_URL))
{
$image_urls[] = $element->src;
}
}
//prepare for JSON
$output = array('title'=>$page_title, 'images'=>$image_urls, 'content'=> $page_body);
echo json_encode($output); //output JSON data
data.images gives the array bu it starts with 0
前置一些虛擬值'$ image_urls'之前在數組中分配它。然後從索引1開始使用。 – techfoobar
JavaScript中沒有這樣的數組,它以索引1開始。您可以將一個空元素放入數組中的'0'點,但該點仍然存在。 – jfriend00