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;
}
精彩评论