我有一个如下所示的数组。我想将它切成两半并从两半创建两个新阵列。但是,如果我使用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][] = '';
}
答案 0 :(得分:2)
这些实际上是数组的数组。主数组是一个关联数组,因此您需要遍历该数组的所有键才能获得切片:
$slice1 = array();
$slice2 = array();
foreach ($array as $key => $value)
{
$slice1[$key] = array_slice($array[$key], ...);
$slice2[$key] = array_slice($array[$key], ...);
}
答案 1 :(得分:1)
array_chunk - 将数组拆分为块
$array1 = array("1" => "green", "2" => "brown", "3" => "blue", "4" => "black");
$array2 = array("5" => "yellow","6" => "orange","7"=> "violet","8"=> "white");
$arraylength1 = ceil(count($array1)/2);
$res1 = array_chunk($array1,$arraylength1);
$arraylength2 = ceil(count($array2)/2);
$res2 = array_chunk($array2,$arraylength2);
echo "<pre>";
print_r($res1);
print_r($res2);
echo "</pre>";
exit;
试试这个。
答案 2 :(得分:1)
我很难理解你需要做的事情,如果是我,我不会使用你的结构填充数组,我会创建一个像这样的数组:
array
0 =>
array
'pic' => string '740' (length=3)
'alt' => string '' (length=0)
1 =>
array
'pic' => string '741' (length=3)
'alt' => string '' (length=0)
2 =>
array
'pic' => string '742' (length=3)
'alt' => string 'Test Caption 1' (length=0)
3 =>
array
'pic' => string '748' (length=3)
'alt' => string 'Test Caption 2' (length=0)
此数组结构将图像属性保持在一起,而不是将它们分开。
tomtheman5有80%,但遗漏了偏移量,并且需要传递的array_slice调用的长度参数才能获得正确的切片。但是他确实提出了一个很好的观点,如果你坚持你的阵列结构,如果你在每个'pic'和'alt'数组中都没有匹配的元素,你可能会遇到问题。如果这不是问题,请考虑以下代码段:
$slice1 = array();
$slice2 = array();
foreach ($array as $key => $value) {
$arrayCount = count($array[$key]);
$arrayHalfCount = ($arrayCount / 2);
$slice1[$key] = array_slice($array[$key], 0, $arrayHalfCount);
$slice2[$key] = array_slice($array[$key], $arrayHalfCount);
}
- 编辑 -
$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']))) {
$currentPic['pic'] = $featuredpic['featured_pic'];
$currentPic['alt'] = (isset($featuredpic['featured_alt'])) ? $featuredpic['featured_alt'] : '';
$showcasepics[] = $currentPic;
}
}
foreach ($lodgepics as $lodgepic) {
if ((isset($lodgepic['accommodation_roomphoto'])) && (!empty($lodgepic['accommodation_roomphoto']))) {
$currentPic['pic'] = $lodgepic['accommodation_roomphoto'];
$currentPic['alt'] = (isset($lodgepic['accommodation_roomname'])) ? $lodgepic['accommodation_roomname'] : '';
$showcasepics[] = $currentPic;
}
}
$showcasepicsCount = count($showcasepics);
$showcasepicsHalfCount = ($showcasepicsCount / 2);
$slice1 = array_slice($showcasepics, 0, $showcasepicsHalfCount);
$slice2 = array_slice($showcasepics, $showcasepicsHalfCount);