开发者

What is an efficient way to split a multidimensional array into a set of arrays based on a value?

I have a multidimensional array like the following:

Array (

       [0] => stdClass Object (
                [name] => StackOverflow
                [image] => CanHelp.jpg
       ) 

       [1] => stdClass Object (
                [name] => AnotherObject
                [image] => SecondImage.jpg
       ) 
)

How can I arrange/split this array into groups based on the first letter of [name]?

i.e. There are about 1,000 items in this array, which I have already ordered alphabetically by [name], however I want to be able to have groups that begin with 'A', 'B', etc.

Like this, for 'A' and 'S':

Array (

       [0] => stdClass Object (
                [name] => AnotherObject
                [image] => SecondImage.jpg
       ) 

       [1] => stdClass Object (
                [name] => AndAnothe开发者_StackOverflowr
                [image] => notImportant.jpg
       )
)

Array (

       [0] => stdClass Object (
                [name] => StackOverflow
                [image] => CanHelp.jpg
       )
)


$split = array();
foreach ($array as $item) {
    $split[$item->name[0]][] = $item;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜