[英]group array of php objects by object property
I have a php object (book) with 3 properties: name, category, description
I then have a list of these book
objects in an array. 我有一个PHP对象(书)有3个属性: name, category, description
然后我有一个数组中的这些book
对象的列表。 I want to create a new associative array for these objects grouped by category
. 我想为按category
分组的这些对象创建一个新的关联数组。
Say I have 4 book objects in an array called $books
假设我在一个名为$books
的数组中有4个图书对象
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
how can I create an associative array called $categories
如何创建一个名为$categories
的关联数组
$categories['cat1'] = array(book1, book2)
$categories['cat2'] = array(book2)
$categories['cat3'] = array(book3)
where book? 哪本书? is the book object and not the word 是书的对象,而不是单词
Like this: 像这样:
foreach($books as $book)
{
$categories[$book->category][] = $book;
}
You can do it with ouzo goodies : 你可以用ouzo好吃的东西来做:
$categories = Arrays::groupBy($books, Functions::extractField('category'));
See: http://ouzo.readthedocs.org/en/latest/utils/arrays.html#groupby 请参阅: http : //ouzo.readthedocs.org/en/latest/utils/arrays.html#groupby
Simply loop the array of objects into a new array with the key being the category: 只需将对象数组循环到一个新数组中,其中键为类别:
$newArray = array();
foreach($array as $entity)
{
if(!isset($newArray[$entity->category]))
{
$newArray[$entity->category] = array();
}
$newArray[$entity->category][] = $entity;
}
Is this what you was looking for ? 这是你在找什么?
Explanation of the code: 代码说明:
/*
* Create a new blank array, to store the organized data in.
*/
$newArray = array();
/*
* Start looping your original dataset
*/
foreach($array as $entity)
{
/*
* If the key in the new array does not exists, set it to a blank array.
*/
if(!isset($newArray[$entity->category]))
{
$newArray[$entity->category] = array();
}
/*
* Add a new item to the array, making shore it falls into the correct category / key
*/
$newArray[$entity->category][] = $entity;
}
I had a similar problem, but a bit more complicated with wordpress and metavalues/metakeys (where $results was an array of associative arrays fetched from a $wpdb->get_results() query. 我有一个类似的问题,但有点更复杂的wordpress和metavalues / metakeys(其中$ results是从$ wpdb-> get_results()查询获取的关联数组的数组。
This was my solution adapted to your problem: 这是我的解决方案适应您的问题:
$categories = array();
foreach ($results as $row) {
$id = $row['category'];
$description = $row['category'];
$name = $row['name']
if (!isset($categories[$id])) {
$categories[$id] = array();
}
$categories[$id] = array_merge($categories[$id], 'description'=>$description , 'name'=>$name);
}
Then you can run another for loop to get each array from the categories array: 然后你可以运行另一个for循环来从categories数组中获取每个数组:
foreach ($categories as $category) {
var_dump($category);
}
如果要通过从具有或不具有其他参数的特定方法获取键来对对象进行分组,则可以使用此库方法:
Arr::groupObjects(array $objects, string $method, ...$args): array
$categories = array();
for ($i = 0; $i < count($books); $i++){
if (isset($categories[$books[$i]->category]) == false)
$categories[$books[$i]->category] = array();
$categories[$books[$i]->category][] = $books[$i]
}
cheers 干杯
Try this: 尝试这个:
$categories = array();
foreach ($books as $book){
if (!array_key_exists($book->category , $categories))
$categories[$book->category] = array();
$categories[$book->category][] = $book;
}
This should works: 这应该有效:
$categories = array();
foreach ($books as $book) {
$categories[$book['category']][] = $book;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.