按对象属性对php对象数组进行分组
我有一本php对象(书),它有3个属性:按对象属性对php对象数组进行分组,php,Php,我有一本php对象(书),它有3个属性:name、category、description 然后,我有一个数组中这些book对象的列表。 我想为这些按类别分组的对象创建一个新的关联数组 假设我在一个名为$books name category description ================================== book1 cat1 this is book 1 book2 cat1 this is book 2 book3 cat2
name、category、description
然后,我有一个数组中这些book
对象的列表。
我想为这些按类别分组的对象创建一个新的关联数组
假设我在一个名为$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=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]
}
对于($i=0;$icategory])==false)
$categories[$books[$i]->categority]=array();
$categories[$books[$i]->categories][]=$books[$i]
}
像这样干杯:
foreach($books as $book)
{
$categories[$book->category][] = $book;
}
只需将对象数组循环到一个新数组中,其中键为类别:
$newArray = array();
foreach($array as $entity)
{
if(!isset($newArray[$entity->category]))
{
$newArray[$entity->category] = array();
}
$newArray[$entity->category][] = $entity;
}
这就是你要找的吗
守则解释:
/*
* 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;
}
试试这个:
$categories = array();
foreach ($books as $book){
if (!array_key_exists($book->category , $categories))
$categories[$book->category] = array();
$categories[$book->category][] = $book;
}
这应该是有效的:
$categories = array();
foreach ($books as $book) {
$categories[$book['category']][] = $book;
}
您可以通过以下方式完成:
请参阅:我遇到了类似的问题,但wordpress和metavalues/metakeys(其中$results是从$wpdb->get_results()查询中获取的关联数组)的问题要复杂一些
这是我针对您的问题提出的解决方案:
$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);
}
然后,您可以运行另一个for循环,从categories数组中获取每个数组:
foreach ($categories as $category) {
var_dump($category);
}
如果要通过从特定方法获取键(带或不带附加参数)对对象进行分组,可以使用方法:
与JavaScript不同,您无法使用数组[propertyName]
语法访问对象属性。如果您找到了这个答案,并且没有立即意识到它在做什么,那么它正在创建一个新数组(名为$categories
)对于$book->category
的每个值都有一个键。每个键的值都是一个$book
对象数组,这些对象对应于$book->category
。如果在这个注释中它看起来不像泥,我会制作一个ASCII图片。如果你想对类别排序,那就需要额外的:ksort($categories);
请参阅
foreach ($categories as $category) {
var_dump($category);
}
Arr::groupObjects(array $objects, string $method, ...$args): array