2010-06-01 12 views
1

我有類似值的數組:陣列filteration PHP

Array 
(
    [0] => Array 
     (
      [parent] => Basic 
      [parentId] => 1 
      [child] => Birthday 
      [childId] => 2 
     ) 

    [1] => Array 
     (
      [parent] => Basic 
      [parentId] => 1 
      [child] => Gender 
      [childId] => 3 
     ) 

    [2] => Array 
     (
      [parent] => Geo 
      [parentId] => 10 
      [child] => Current City 
      [childId] => 11 
     ) 

    [3] => Array 
     (
      [parent] => Known me 
      [parentId] => 5 
      [child] => My personality 
      [childId] => 7 
     ) 

    [4] => Array 
     (
      [parent] => Known me 
      [parentId] => 5 
      [child] => Best life moment 
      [childId] => 8 
     ) 
) 

而且我要這樣,基於指數,而最終的結果他們的過濾會像過濾此陣:

Array 
(
    [0] => Array 
     (
      [parent] => Basic 
      [parentId] => 1 
      [child] => Array 
       (
        [0] => Birthday 
        [1] => Gender 
       ) 

     ) 

    [1] => Array 
     (
      [parent] => Geo 
      [parentId] => 10 
      [child] => Array 
       (
        [0] => Current City     
       ) 

     ) 

    [2] => Array 
     (
      [parent] => Known me 
      [parentId] => 5 
      [child] => Array 
       (
        [0] => My personality 
        [1] => Best life moment 
       ) 

     ) 
) 

對於我的編碼:

$filter = array(); 
$f = 0; 
for ($i=0; $i<count($menuArray); $i++) { 
    $c = 0; 
    for($b = 0; $b < count($filter); $b++){ 

     if($filter[$b]['parent'] == $menuArray[$i]['parent']){ 
      $c++; 
     } 
    } 
    if ($c == 0) { 
     $filter[$f]['parent'] = $menuArray[$i]['parent']; 
     $filter[$f]['parentId'] = $menuArray[$i]['parentId']; 
     $filter[$f]['child'][] = $menuArray[$i]['child']; 
     $f++; 
    } 
} 

但它重新sults:

Array 
(
    [0] => Array 
     (
      [parent] => Basic 
      [parentId] => 1 
      [child] => Array 
       (
        [0] => Birthday 
       ) 

     ) 

    [1] => Array 
     (
      [parent] => Geo 
      [parentId] => 10 
      [child] => Array 
       (
        [0] => Current City 
       ) 

     ) 

    [2] => Array 
     (
      [parent] => Known me 
      [parentId] => 5 
      [child] => Array 
       (
        [0] => My personality 
       ) 

     ) 
) 

任何人都可以指出我失蹤的LOC?

+0

直接回答你的問題是,當你發現已經定義了一個匹配的父你不加入的孩子。除了說'C++',你還應該說'filter [$ b] ['child'] [] = $ menuArray [$ i] ['child']' – 2010-06-02 01:51:49

回答

1

嘗試:該陣列的索引是否與的ID

Array 
(
    [1] => Array 
     (
      [parent] => Basic 
      [parentId] => 1 
      [child] => Array 
       (
        [2] => Birthday 
        [3] => Gender 
       ) 

     ) 

    [10] => Array 
     (
      [parent] => Geo 
      [parentId] => 10 
      [child] => Array 
       (
        [11] => Current City     
       ) 

     ) 

    [5] => Array 
     (
      [parent] => Known me 
      [parentId] => 5 
      [child] => Array 
       (
        [7] => My personality 
        [8] => Best life moment 
       ) 

     ) 
) 

說明:

$filter = array(); 
foreach ($menuArray as $menu) { 
    if (!array_key_exists($menu['parent_id'], $filter)) { 
    $filter[$menu['parent_id']] = array(
     'parent' => $menu['parent'], 
     'parent_id' => $menu['parent_id'], 
     'child' => array() 
    ); 
    } 
    $filter[$menu['parent_id']]['child'][$menu['child_id']] = $menu['child']; 
} 

這將產生像陣列。你不能循環使用for循環,但你可以正確地使用foreach ($filter as $parent_id=>$parent)。如果你願意,你可以改變線4的我的代碼$filter['key_' . $menu['parent_id']]迫使一個字符串和一個for循環將工作

0

下一條代碼是完全未經測試,並基於這樣的理念,它的通過parentId的排序。

$filter = array(); 
$option = null; 
for($i = 0; $i < count($menuArray); $i++) { 
    if(count($filter) < 1 || $filter[count($filter)-1]['parentId'] != $menuArray['parentId']) { 
     if($option != null) { 
      $filter[] = $option; 
     } 

     $option = array(
      "parent" => $menuArray[$i]['parent'], 
      "parentId" => $menuArray[$i]['parentId'], 
      "child"  => array() 
     ); 
    } 

    $option['child'][] = $menuArray[$i]['child']; 
} 

$filter[] = $option; // one last time, because we left the loop already. 
unset($option); // we don't need it anymore. 

它做什麼,它爲每一個父$option。只要我們打的未來parentId我們添加當前$option$filter陣列並創建一個新$option對象。

所有的孩子發展得越來越添加到當前$option