特定键的arange数组结果

时间:2012-03-26 19:09:00

标签: php arrays multidimensional-array

这是我当前的数组print_r

Array(
  [0] => stdClass Object
        (
            [category] => Cat1
             [title] => Title1
         )

  [1] => stdClass Object
        (
            [category] => Cat2
             [title] => Title2


         )
  [2] => stdClass Object
        (

            [category] => Cat2
             [title] => Title3
         )
)

但我需要我的print_r喜欢

Array(
  [0] => stdClass Object
        (
            [category] => Cat1
             [title] => Title1
         )

)

Array(

  [0] => stdClass Object
        (
            [category] => Cat2
             [title] => Title2


         )
  [1] => stdClass Object
        (

            [category] => Cat2
             [title] => Title3
         )
)

所以我无法列出来自cat1的所有新闻,而不是像手风琴

那样列出类别名称的猫2
cat1:
 Title1
cat2:
 Title2
 Title3
cat3:
 Title4
 Title5

任何帮助表示赞赏。谢谢!

1 个答案:

答案 0 :(得分:1)

类似的东西:

$array = array();
foreach($yourCurrentArray as $item) {
    $category = $item->category;
    if(isset($array[$category]) == false) {
        $array[$category] = array();
    }
    $array[$category][] = $item;
}
print_r($array);