2010-12-06 133 views
1

假设我有以下形式的多维数组:帮助PHP循环

array 
(
    array('Set_ID' => 1, 'Item_ID' => 17, 'Item_Name' = 'Whatever'), 
    array('Set_ID' => 1, 'Item_ID' => 18, 'Item_Name' = 'Blah'), 
    array('Set_ID' => 2, 'Item_ID' => 19, 'Item_Name' = 'Yo') 
) 

阵列具有多个子阵列,但是这是在设定的基本form--项目。

我怎么能遍历这个数组,这样我可以回响在每个项目连同所有项目,像这样设置的数量:

Set 1 has 2 Items: 17: Whatever and 18: Blah 
Set 2 has 1 Items: 19: Yo 

我知道,这可能有两个做循环 - 一个构建数组,另一个循环遍历数组。不过,我只想用一个循环来完成这一切。

在你的答案,你应该假设有两种显示功能

display_set($id, $count) //echo's "Set $id has $count Items" 
display_item($id, $name) //echo's "$id: $name" 

更新:忘了提,该数据是由SET_ID,因为它从SQL

+5

听起来像作业。 – 2010-12-06 00:02:55

+0

@DeathMagus:我刚刚说完看完最后一点...... – BoltClock 2010-12-06 00:03:30

+0

呃,这不是作业。这是一个我从未遇到过的循环结构。我打算用我自己的尝试来更新这篇文章,我只想看看我是否可以做到这一点 – babonk 2010-12-06 00:05:41

回答

0

权,所有的例子下面依赖于一组有序的OP指出它最初订购的,但如果需要排序功能可以是:使用单回路

// Sort set in to order 
usort($displaySet, 
     create_function('$a,$b', 
         'return ($a['Set_ID'] == $b['Set_ID'] 
           ? ($a['Set_ID'] == $b['Item_ID'] 
            ? 0 
            : ($a['Item_ID'] < $b['Item_ID'] 
            ? -1 
            : 1)) 
           : ($a['Set_ID'] < $b['Set_ID'] ? -1 : 1));')); 

直例如:

// Initialise for the first set 
$cSetID = $displaySet[0]['Set_ID']; 
$cSetEntries = array(); 

foreach ($displaySet as $cItem) { 
    if ($cSetID !== $cItem['Set_ID']) { 
    // A new set has been seen, display old set 
    display_set($cSetID, count($cSetEntries)); 
    echo ": " . implode(" and ", $cSetEntries) . "\n"; 
    $cSetID = $cItem['Set_ID']; 
    $cSetEntries = array(); 
    } 

    // Store item display for later 
    ob_start(); 
    display_item($cItem['Item_ID'], $cItem['Item_Name'); 
    $cSetEntries[] = ob_get_clean(); 
} 

// Perform last set display 
display_set($cSetID, count($cSetEntries)); 
echo ": " . implode(" and ", $cSetEntries) . "\n"; 

使用递归函数它可能是这样的:

// Define recursive display function 
function displayItemList($itemList) { 
    if (!empty($itemList)) { 
    $cItem = array_shift($itemList); 
    display_item($cItem['Item_ID'], $cItem['Item_Name'); 

    if (!empty($itemList)) { 
     echo " and "; 
    } 
    } 

    displayItemList($itemList); 
} 

// Initialise for the first set 
$cSetID = $displaySet[0]['Set_ID']; 
$cSetEntries = array(); 

foreach ($displaySet as $cItem) { 
    if ($cSetID !== $cItem['Set_ID']) { 
    // A new set has been seen, display old set 
    display_set($cSetID, count($cSetEntries)); 
    echo ": "; 
    displayItemList($cSetEntries); 
    echo "\n"; 
    $cSetID = $cItem['Set_ID']; 
    $cSetEntries = array(); 
    } 

    // Store item for later 
    $cSetEntries[] = $cItem; 
} 

// Perform last set display 
display_set($cSetID, count($cSetEntries)); 
echo ": "; 
displayItemList($cSetEntries); 
echo "\n"; 

有趣的是,它可以是一个单一的递归函数:

function displaySetList($setList, $itemList = NULL) { 
    // First call, start process 
    if ($itemList === NULL) { 
    $itemList = array(array_shift($setList)); 
    displaySetList($setList, $itemList); 

    return; 
    } 

    // Check for display item list mode 
    if ($setList === false) { 
    // Output first entry in the list 
    $cItem = array_shift($itemList); 
    display_item($cItem['Item_ID'], $cItem['Item_Name']); 

    if (!empty($itemList)) { 
     // Output the next 
     echo " and "; 
     displaySetList(false, $itemList); 
    } else { 
     echo "\n"; 
    } 

    return; 
    } 

    if (empty($setList) || $setList[0]['Set_ID'] != $itemList[0]['Set_ID']) { 
    // New Set detected, output set 
    display_set($itemList[0]['Set_ID'], count($itemList)); 
    echo ": "; 
    displaySetList(false, $itemList); 
    $itemList = array(); 
    } 

    // Add next item and carry on 
    $itemList[] = array_shift($setList); 
    displaySetList($setList, $itemList); 
} 

// Execute the function 
displaySetList($displaySet); 

注意,递归这里的例子是非常低效的,双回路是最快的。

0
<?php 
$sets = array(); 
foreach ($items as $item) 
{ 
    if (!array_key_exists($item['Set_ID'], $sets)) 
    { 
     $sets[$item['Set_ID']] = array(); 
    } 
    $sets[$item['Set_ID']][] = $item; 
} 
foreach ($sets as $setID => $items) 
{ 
    echo 'Set ' . $setID . ' has ' . count($items) . ' Items: '; 
    foreach ($items as $item) 
    { 
     echo $item['Item_ID'] . ' ' . $item['Item_Name']; 
    } 
} 
?> 

像这样的东西整理我猜?

编辑: 我发布后,我看到了添加的显示功能。但你明白了。

0

直到我们知道集合中有多少,才会打印出任何项目,这使得这很困难。在某个时候,我们需要做一些缓冲或者回溯。但是,如果我被允许内部循环,并集是“主”数组中连续的,那么一些黑客周围:

$set = 0; 
$items; 

foreach ($arr as $a) { 
    if ($a['Set_ID'] != $set) { 
     if ($set != 0) { 
      display_set($set, count($items)); 
      foreach ($items as $i) 
       display_item($i) 
     } 
     $set = $a['Set_ID']; 
     $items = array(); 
    } 
    $items[] = $a; 
} 
0

如何:

$previous_set = false; 

$items = ''; 
$item_count = 0; 

foreach ($rows as $row) 
{ 
if ($row['Set_ID'] != $previous_set) 
{ 
    if ($previous_set) 
    { 
    echo display_set($row['Set_ID'], $item_count); 
    echo $items; 
    } 
    $previous_class = $row['Set_ID']; 
    $item_count = 0; 
    $items = ''; 
} 
$items .= display_item($row['Item_ID'], $row['Title']); 
$item_count++; 

} 
echo display_set($row['Set_ID'], $item_count); 
echo $items;