我有以下数组:
$people = array(
array( 'name'=>'Sarah',
'gender'=>'F'),
array( 'name'=>'Darren',
'gender'=>'M'),
array( 'name'=>'John',
'gender'=>'M'),
array( 'name'=>'Phil',
'gender'=>'M'),
array( 'name'=>'Alice',
'gender'=>'F'),
array( 'name'=>'Sam',
'gender'=>'M'),
);
我想让它以2列结构显示如下:
Sarah | Darren
John | Phil
Alice | Sam
我正在使用array_chunk并循环遍历如下:
foreach(array_chunk($people, 2, true) as $array)
{
?>
<div class="left"><?php echo $array[0]['name']; ?></div>
<div class="right"><?php echo $array[1]['name']; ?></div>
<?php
}
以上说法不起作用,因为它说:Undefined offset: 0
print_r($array)
的值为:
Array ( [0] => Array ( [name] => Sarah [gender] => F ) [1] => Array ( [name] => Darren [gender] => M ) )
答案 0 :(得分:1)
答案 1 :(得分:0)
我实现了这样的输出:
$count = 0;
foreach($people as $person) {
if($count % 2 == 0) {
echo $person['name'] . ' | ';
} else {
echo $person['name'] . '<br /><br />';
}
$count++;
}