我有一個數組,我需要在多級數組中排序此數組。我試圖按照字段對其進行分組,但我可以使其工作。這裏是陣列我有什麼,我想是否可以對這個數組進行分組?
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;
}
任何建議都會很棒。
數組中的數據是否來自數據庫?在這種情況下,更改查詢可能會更簡單。 – Jocelyn 2012-07-07 13:10:17
我使用preg_match_all()從HTML內容中獲取這些數據,但無法更改:( – Anuj 2012-07-07 13:14:10