问题
Please compare the CURRENT
and DESIRED
results below to see what I'm trying to do.
$arr=array(
array('list_name'=>'Food', 'list_item'=>'bread'),
array('list_name'=>'Food', 'list_item'=>'meat'),
array('list_name'=>'Drinks', 'list_item'=>'water'),
array('list_name'=>'Drinks', 'list_item'=>'milk')
);
foreach($arr as $row){
echo $row['list_name'].'<br>';
echo $row['list_item'].'<br>';
echo '<br>___<br>';
}
CURRENT RESULT
Food
bread
___
Food
meat
___
Drinks
water
___
Drinks
milk
___
DESIRED RESULT
Food
1.bread
2.meat
___
Drinks
1.water
2.milk
___
UPDATE
More complex, yet more generalizable example:
$arr=array(
array('list_name'=>'Food', 'list_item1'=>'bread', 'list_item2' => 2001),
array('list_name'=>'Food', 'list_item1'=>'meat', 'list_item2' => 2002),
array('list_name'=>'Drinks', 'list_item1'=>'water', 'list_item2' => 2003),
array('list_name'=>'Drinks', 'list_item1'=>'milk', 'list_item2' => 2004)
);
DESIRED RESULT
Food
1.bread, 2001
2.meat, 2002
___
Drinks
1.water, 2003
2.milk, 2004
___
回答1:
You could iterate over the array and add the item data to a new array (one for each list_name
).
For example:
$new_array = array();
foreach ($arr as $row)
{
$new_array[$row['list_name']][] = $row['list_item'];
}
foreach ($new_array as $name => $items)
{
echo $name . '<br>';
foreach ($items as $index => $item)
{
echo ($index + 1) . '. ' . $item . '<br>';
}
echo '___<br>';
}
With your update:
$new_array = array();
foreach ($arr as $row)
{
$name = $row['list_name'];
unset($row['list_name']);
$new_array[$name][] = $row;
}
foreach ($new_array as $name => $items)
{
echo $name . '<br>';
foreach ($items as $index => $item)
{
echo ($index + 1) . '. ' . implode(', ', $item) . '<br>';
}
echo '___<br>';
}
回答2:
$arr=array(
array('list_name'=>'Food', 'list_item'=>'bread'),
array('list_name'=>'Food', 'list_item'=>'meat'),
array('list_name'=>'Drinks', 'list_item'=>'water'),
array('list_name'=>'Drinks', 'list_item'=>'milk')
);
$names = array();
foreach ( $arr as $item){
$name = $item['list_name'];
if(!in_array($name, $names)){
$names[] = $name;
echo $name ."<BR>";
}
echo $item['list_item']."<BR>";
}
回答3:
One common recipe for this is to store the current heading/section into a variable as you loop over the list. Then each time you encounter a new heading, you echo that heading, and re-start your numbering (or close your HTML <ol>
tag, etc).
$current_section = NULL;
foreach ( $arr as $row )
{
if ( $current_section != $row['list_name'] )
{
// Begin new section
echo $row['list_name'];
// Remember current section
$current_section != $row['list_name'];
}
// Output current item
echo $row['list_item'];
}
Note that this approach will only work if your list is sorted such that all "food" items come together, then all "drinks", etc.
Otherwise, you can use the alternative approach of re-structuring your array so that there is a sub-array for each section, and then using a pair of nested loops:
$new_array = array();
foreach ($arr as $row)
{
$new_array[$row['list_name']][] = $row['list_item'];
}
foreach ( $new_array as $section_name => $items )
{
echo $section_name;
foreach ( $items as $row )
{
echo $row;
}
}
回答4:
Ok, I made a mistake earlier but I tested this and it does work.
<?php
$food = array('bread', 'meat');
$drink = array('water', 'milk');
$object = array('food' => $food, 'drink' => $drink);
foreach($object as $row){
echo $row[0].'<br>';
echo $row[1].'<br>';
echo '<br>___<br>';
}
?>
来源:https://stackoverflow.com/questions/15178829/php-foreach-loop-show-each-list-item-while-only-showing-list-name-once