我有像:
这样的数组 foreach($Pics AS $Allpics) {
print $Allpics;
}
Result my values:
string(40) "760_e7c5c3202c778318fdf92f406da31742.jpg"
string(40) "760_00f500b6398b4d8a0cde299730f57148.gif"
string(40) "760_54b1bb6895b636f45c56911be4f67c11.png"
string(40) "760_05986e1f46651698a8aa4f8ed17ab070.jpg"
我需要将Split数组值分成两列!
喜欢:
[column 1] [column 2]
760_e7c5c3202c778318fdf92f406da31742.jpg 760_54b1bb6895b636f45c56911be4f67c11.png
760_00f500b6398b4d8a0cde299730f57148.gif 760_05986e1f46651698a8aa4f8ed17ab070.jpg
Html结果如:
<div class='row'>
<div class='col-sm-6'>
760_e7c5c3202c778318fdf92f406da31742.jpg
760_54b1bb6895b636f45c56911be4f67c11.png
</div>
<div class='col-sm-6'>
760_00f500b6398b4d8a0cde299730f57148.gif
760_05986e1f46651698a8aa4f8ed17ab070.jpg
</div>
</div>
感谢你帮助我的朋友们!
答案 0 :(得分:0)
array_chunk($arrays,2);
答案 1 :(得分:0)
请参阅代码,它可能有助于您实现目标
$arrays = array('Like' ,'Starts' , 'Moons', 'Skys');
$odd = array();
$even = array();
$i=1;
foreach($arrays as $val)
{
if($i%2==0)
$even[] = $val;
else
$odd[] = $val;
$i++;
}
print_r($odd);
print_r($even);
答案 2 :(得分:0)
使用数组块...
$arrays = ["Like" ,"Starts" , "Moons", "Skys"];
$arrays = array_chunk($arrays,2);
<div class='row'>
<div class='col-sm-6'>
<?php foreach ($arrays[0] as $key => $value) {
echo $value."<br>";
} ?>
</div>
<div class='col-sm-6'>
<?php foreach ($arrays[1] as $key1 => $value1) {
echo $value1."<br>";
} ?>
</div>
</div>
答案 3 :(得分:0)
在这里,您可以指定列数,而算法将完成其余的
echo "<div class='row'>";
// cant be greater than 12 because bootstrap only supp 12 columns
$columns = 2;
$arrays = array('Like' ,'Starts' , 'Moons', 'Skys');
$array_m = round(count($arrays) / $columns);
for ($i = 0; $i < $columns; $i++){
echo "<div class='col-sm-".round(12/$columns)."'>";
for ($i2= $i * $array_m; $i2 < ($i+1==$columns? count($arrays) : $array_m) ; $i2++) {
echo $arrays[$i2] . '<br>';
}
echo "</div>";
}
echo "</div>";
答案 4 :(得分:0)
Please try this code
$arrays = array('Like' ,'Stars' , 'Moons', 'Skys');
$arraychunk=array_chunk($arrays,2);
?>
<div class='row'>
<?php
foreach($arraychunk as $item)
{
?><div class='col-sm-6'><?php
foreach($item as $arr)
{
echo "$arr"."<br>";
}
?></div><?php
}
?>
</div>
答案 5 :(得分:0)
你可以使用array_chunk(),但问题是,如果记录是奇数,那么array_chunk()创建第三个数组,所以,你错过了最后一个记录。
这很简单...... 使用array_slice()来避免逻辑错误。
$Allpics = array("nature", "trees", "beauty","funny", "fun");
//counting number of records
$countRecords = count($Allpics);
//dividing array in to two array
$col1 = array_slice($Allpics, 0, $countRecords/2 + 0.5);
$col2 = array_slice($Allpics, $countRecords/2 + 0.5, $countRecords);
//making two columns
$row = array("column 1" => $col1, "column 2" => $col2);
print_r($row);
//输出
Array(
[column1] => Array(
[0] => nature
[1] => trees
[2] => beauty
) [column2] => Array(
[0] => funny
[1] => fun
)
)
此代码将创建两列,记录为奇数,第一列包含3条记录,第二列包含2条记录。如果记录是偶数,那么它将创建两个相等的列。
如果你想从$ Allpics获得相同的数组键,那么在array_slice()
中使用true 了解详情