我有一个PHP对象(book),包含3个属性:name,category,description
然后我有一个包含这些book
对象的数组列表。
我想为这些对象创建一个按category
分组的新关联数组。
假设我有4个名为$books
的书籍对象在一个数组中。
name category description
==================================
book1 cat1 this is book 1
book2 cat1 this is book 2
book3 cat2 this is book 3
book4 cat3 this is book 4
我该如何创建一个名为$categories
的关联数组?
$categories['cat1'] = array(book1, book2)
$categories['cat2'] = array(book2)
$categories['cat3'] = array(book3)
book? 是指书本对象而非单词
$categories
的新数组,并为每个$book->category
的值创建一个键。每个键的值是对应于该$book->category
的$book
对象数组。如果不是在该评论中看起来像泥巴,我会画一个ASCII图片的。 - Travis Hohlksort($categories);
。请参见http://php.net/manual/en/function.ksort.php。 - Travis Hohl