我有一個數組,顯示如下。我想把它分成兩半,並從兩半中創建兩個新陣列。但是,如果我使用array_slice,它總是返回null ...我不明白爲什麼。無法切片數組 - 我需要將數組分割成一半
array
'pic' =>
array
0 => string '740' (length=3)
1 => string '741' (length=3)
2 => string '742' (length=3)
3 => string '748' (length=3)
'alt' =>
array
0 => string '' (length=0)
1 => string '' (length=0)
2 => string 'Test caption 1' (length=14)
3 => string 'Test caption 2' (length=14)
我需要的陣列應該保留鍵,只是切成兩半,所以我有兩個免費的一半。例如,上半年應該是這樣的:
array
'pic' =>
array
0 => string '740' (length=3)
1 => string '741' (length=3)
'alt' =>
array
0 => string '' (length=0)
1 => string '' (length=0)
和下半年:
array
'pic' =>
array
0 => string '742' (length=3)
1 => string '748' (length=3)
'alt' =>
array
0 => string 'Test caption 1' (length=14)
1 => string 'Test caption 2' (length=14)
感謝
PS - 邁克,這是我使用的是什麼使陣列,實際上它是由其他陣列構成的:
$lodgepics = get_field('accommodation_rooms');
$featuredpics = get_field('featured_pics');
$showcasepics = array();
foreach ($featuredpics as $featuredpic) {
if (isset($featuredpic['featured_pic'])&&!empty($featuredpic['featured_pic'])) $showcasepics[pic][] = $featuredpic['featured_pic'];
if (isset($featuredpic['featured_alt'])) $showcasepics[alt][] = $featuredpic['featured_alt'];
else $showcasepics[alt][] = '';
}
foreach ($lodgepics as $lodgepic) {
if(isset($lodgepic['accommodation_roomphoto'])&&!empty($lodgepic['accommodation_roomphoto'])) $showcasepics[pic][] = $lodgepic['accommodation_roomphoto'];
if(isset($lodgepic['accommodation_roomname'])&&!empty($lodgepic['accommodation_roomname'])) $showcasepics[alt][] = $lodgepic['accommodation_roomname'];
else $showcasepics[alt][] = '';
}
嗨邁克,感謝您的建議是非常有幫助的 我已經包含了我在原始文章中構建數組的代碼,可以檢查它嗎?也許我可以簡單地調整數組,使其更容易切片和使用 - 謝謝 – unfulvio 2012-01-13 06:18:43
http://stackoverflow.com/questions/8849009/create-merging-multidimensional-array-with-key-pairs-from-different-keys- two-d – unfulvio 2012-01-13 11:18:21
@Fulvio:用建議的方式更新了文章,以建立您的圖像數組。 – 2012-01-13 19:18:54