2012-07-07 40 views
0

我有一個數組,我需要在多級數組中排序此數組。我試圖按照字段對其進行分組,但我可以使其工作。這裏是陣列我有什麼,我想是否可以對這個數組進行分組?

Array 
(
    [0] => Array 
     (
      [id] => sports 
      [title] => this is sports 
     ) 

    [1] => Array 
     (
      [id] => cricket 
      [title] => this is cricket 
      [under] => sports 
     ) 

    [2] => Array 
     (
      [id] => batsman 
      [title] => this is batsman 
      [under] => cricket 
     ) 

    [3] => Array 
     (
      [id] => sachin 
      [title] => this is sachin 
      [under] => batsman 
     ) 

    [4] => Array 
     (
      [id] => football 
      [title] => this is football 
      [under] => sports 
     ) 

    [5] => Array 
     (
      [id] => ronaldo 
      [title] => this is ronaldo 
      [under] => football 
     ) 

) 

我需要組數組,並使它像這樣

Array(
    [0] => Array(
     [id] => Array(
      [sports] => Array(
       [cricket] => Array(
        [batsman] => sachin 
       ) 
       [football] => fun 
      ) 
     ) 
    ) 
) 

我試着像這樣的例子,但它不工作

foreach($my_array as $item) { 
    //group them by under 
    $my_grouped_array[$item['under']][] = $item; 
} 

任何建議都會很棒。

+0

數組中的數據是否來自數據庫?在這種情況下,更改查詢可能會更簡單。 – Jocelyn 2012-07-07 13:10:17

+0

我使用preg_match_all()從HTML內容中獲取這些數據,但無法更改:( – Anuj 2012-07-07 13:14:10

回答

0

我認爲這是這樣做的最直接的方式:

function getChildren($entry,$by_parent){ 
    $children = array(); 
    if (isset($by_parent[$entry['id']])){ 
     foreach ($by_parent[$entry['id']] as $child){ 
      $id = $child['id']; 
      $children[$id] = getChildren($child,$by_parent); 
     } 
    } 
    return $children; 
} 

$by_parent = array(); 
$roots = array(); 
foreach ($array as $entry){ 
    if (isset($entry['under'])){ 
     $by_parent[$entry['under']][] = $entry; 
    } else { 
     $roots[] = $entry; 
    } 
} 
$result = array(); 
foreach ($roots as $entry){ 
    $id = $entry['id']; 
    $result[$id] = getChildren($entry,$by_parent); 
} 
$results = array(array('id'=>$results)); 

注:這不是相當問題中指定的格式,但問題並沒有定義如何處理具有相同父代的多個葉節點,並且這應該更容易遍歷,因爲它更一致。

-1

使用PHP對象:

function populateArray($my_array) { 
    //Populate the array 
    while ($my_array as $item) { 
      $array[$item->id]['id'] = $obj->id; 
      $array[$item->id]['name'] = $obj->name; 
     } 
    return $array; 
    } 

$a = populateArray($array);  
echo $a[0]['id'].'<br />'; 
echo $a[0]['name'].'<br />'; 

或使用新的foreach

+0

這看起來不像是因爲至少有兩個原因會起作用($ obj undefined並指向$ item作爲對象),即使這樣做,也不會給出問題所要求的層次結構。 – Braiba 2012-07-09 07:57:38

+0

更改$ obj whit $ my_array – Julien 2012-07-09 08:42:12

+0

$ my_array也是一個數組,不是對象,即使數組可以被處理作爲一個對象,它仍然不會有這些屬性,Plus'as'是foreach的語法,在while循環中不起作用,即使你解決了所有這些問題,它仍然不能回答這個問題。 – Braiba 2012-07-09 09:24:58

0

我寫了一個遞歸函數,你想要做什麼,但要注意裸露的,如果你有一個分支的一個以上的最後一個元素只有第一個將被保存。

這裏的功能:

function rearrange(&$result, $my_array, $element = NULL) 
{ 
     $found = 0; 
     $childs = 0; 
     foreach($my_array as $one) if(@$one['under'] == $element) 
     { 
       $found++; 
       if(! is_array($result)) $result = array(); 
       $result[$one['id']] = $one['id']; 

       $childs += rearrange($result[$one['id']], $my_array, $one['id']); 
     } 
     if(! $childs AND is_array($result)) 
       $result = reset($result); 

     return $found; 
} 

你可以把它像:

$result = array(array('id' => array())); 
rearrange($result[0]['id'], $my_array); 
print_r($result); 
相關問題