我有一个像下面这样的收藏品。
包括此集合的所有嵌套子类别。
我想按"type“列在"categoryprop”下分组。
我该怎么做呢?
[
{
"id":1,
"parrent_id":null,
"order":0,
"name":"Home Page",
"slug":"\/",
"type":"home-page",
"status":"1",
"menu":"0",
"megamenu":"0",
"megamcol":0,
"megamrow":0,
"menuheadershow":"0",
"menufootershow":"0",
"menuiconbg":"",
"menuicon":"",
"created_at":null,
"updated_at":null,
"subcategory":[
],
"categoryprop":[
]
},
{
"id":2,
"parrent_id":null,
"order":1,
"name":"Hosting",
"slug":"hosting",
"type":"hosting",
"status":"1",
"menu":"0",
"megamenu":"1",
"megamcol":2,
"megamrow":4,
"menuheadershow":"1",
"menufootershow":"1",
"menuiconbg":null,
"menuicon":null,
"created_at":null,
"updated_at":null,
"subcategory":[
{
"id":3,
"parrent_id":2,
"order":0,
"name":"Web Hosting",
"slug":"web-hosting",
"type":"hosting",
"status":"1",
"menu":"0",
"megamenu":"0",
"megamcol":0,
"megamrow":0,
"menuheadershow":"0",
"menufootershow":"0",
"menuiconbg":"primary-bg",
"menuicon":"fas fa-server",
"created_at":null,
"updated_at":null,
"subcategory":[
{
"id":15,
"parrent_id":3,
"order":0,
"name":"Web Hosting",
"slug":"web-hosting",
"type":"hosting",
"status":"1",
"menu":"0",
"megamenu":"0",
"megamcol":0,
"megamrow":0,
"menuheadershow":"0",
"menufootershow":"0",
"menuiconbg":"primary-bg",
"menuicon":"fas fa-server",
"created_at":null,
"updated_at":null,
"subcategory":[
],
"categoryprop":[
{
"id":3,
"category_id":15,
"order":0,
"type":"menudescription",
"name":"Men\u00fc A\u00e7\u0131klamas\u0131",
"created_at":"2022-09-09T16:37:02.000000Z",
"updated_at":"2022-09-09T16:37:02.000000Z",
"categorycontent":[
{
"id":5,
"categoryprop_id":3,
"title":"Bu bir denemedir",
"content":"Bu bir deneme",
"image":null,
"lang_id":1,
"created_at":"2022-09-09T16:37:02.000000Z",
"updated_at":"2022-09-09T16:37:02.000000Z",
"lang":{
"id":1,
"name":"T\u00fcrk\u00e7e",
"symbol":"TR",
"code":"tr",
"default":1,
"created_at":null,
"updated_at":null
}
},
{
"id":6,
"categoryprop_id":3,
"title":"Bur bir deneme",
"content":"Bu bir deneme",
"image":null,
"lang_id":2,
"created_at":"2022-09-09T16:37:02.000000Z",
"updated_at":"2022-09-09T16:37:02.000000Z",
"lang":{
"id":2,
"name":"English",
"symbol":"EN",
"code":"en_us",
"default":0,
"created_at":null,
"updated_at":null
}
}
]
},
{
"id":4,
"category_id":3,
"order":0,
"type":"menudescription",
"name":"Men\u00fc A\u00e7\u0131klamas\u01312",
"created_at":"2022-09-09T16:37:02.000000Z",
"updated_at":"2022-09-09T16:37:02.000000Z",
"categorycontent":[
{
"id":7,
"categoryprop_id":4,
"title":"Bu bir denemedir",
"content":"Bu bir deneme",
"image":null,
"lang_id":1,
"created_at":"2022-09-09T16:37:02.000000Z",
"updated_at":"2022-09-09T16:37:02.000000Z",
"lang":{
"id":1,
"name":"T\u00fcrk\u00e7e",
"symbol":"TR",
"code":"tr",
"default":1,
"created_at":null,
"updated_at":null
}
},
{
"id":8,
"categoryprop_id":4,
"title":"Bur bir deneme",
"content":"Bu bir deneme",
"image":null,
"lang_id":2,
"created_at":"2022-09-09T16:37:02.000000Z",
"updated_at":"2022-09-09T16:37:02.000000Z",
"lang":{
"id":2,
"name":"English",
"symbol":"EN",
"code":"en_us",
"default":0,
"created_at":null,
"updated_at":null
}
}
]
}
]
}
],
"categoryprop":[
{
"id":1,
"category_id":3,
"order":0,
"type":"menudescription",
"name":"Men\u00fc A\u00e7\u0131klamas\u0131",
"created_at":"2022-09-09T16:37:02.000000Z",
"updated_at":"2022-09-09T16:37:02.000000Z",
"categorycontent":[
{
"id":1,
"categoryprop_id":1,
"title":"Bu bir denemedir",
"content":"Bu bir deneme",
"image":null,
"lang_id":1,
"created_at":"2022-09-09T16:37:02.000000Z",
"updated_at":"2022-09-09T16:37:02.000000Z",
"lang":{
"id":1,
"name":"T\u00fcrk\u00e7e",
"symbol":"TR",
"code":"tr",
"default":1,
"created_at":null,
"updated_at":null
}
},
{
"id":2,
"categoryprop_id":1,
"title":"Bur bir deneme",
"content":"Bu bir deneme",
"image":null,
"lang_id":2,
"created_at":"2022-09-09T16:37:02.000000Z",
"updated_at":"2022-09-09T16:37:02.000000Z",
"lang":{
"id":2,
"name":"English",
"symbol":"EN",
"code":"en_us",
"default":0,
"created_at":null,
"updated_at":null
}
}
]
},
{
"id":2,
"category_id":3,
"order":0,
"type":"menudescription",
"name":"Men\u00fc A\u00e7\u0131klamas\u01312",
"created_at":"2022-09-09T16:37:02.000000Z",
"updated_at":"2022-09-09T16:37:02.000000Z",
"categorycontent":[
{
"id":3,
"categoryprop_id":2,
"title":"Bu bir denemedir",
"content":"Bu bir deneme",
"image":null,
"lang_id":1,
"created_at":"2022-09-09T16:37:02.000000Z",
"updated_at":"2022-09-09T16:37:02.000000Z",
"lang":{
"id":1,
"name":"T\u00fcrk\u00e7e",
"symbol":"TR",
"code":"tr",
"default":1,
"created_at":null,
"updated_at":null
}
},
{
"id":4,
"categoryprop_id":2,
"title":"Bur bir deneme",
"content":"Bu bir deneme",
"image":null,
"lang_id":2,
"created_at":"2022-09-09T16:37:02.000000Z",
"updated_at":"2022-09-09T16:37:02.000000Z",
"lang":{
"id":2,
"name":"English",
"symbol":"EN",
"code":"en_us",
"default":0,
"created_at":null,
"updated_at":null
}
}
]
}
]
},
{
"id":4,
"parrent_id":2,
"order":1,
"name":"Wordpress Hosting",
"slug":"wordpress-hosting",
"type":"hosting",
"status":"1",
"menu":"0",
"megamenu":"0",
"megamcol":0,
"megamrow":0,
"menuheadershow":"0",
"menufootershow":"0",
"menuiconbg":"accent-bg",
"menuicon":"fas fa-box",
"created_at":null,
"updated_at":null,
"subcategory":[
],
"categoryprop":[
]
},
{
"id":5,
"parrent_id":2,
"order":2,
"name":"Linux Hosting",
"slug":"linux-hosting",
"type":"hosting",
"status":"1",
"menu":"0",
"megamenu":"0",
"megamcol":0,
"megamrow":0,
"menuheadershow":"0",
"menufootershow":"0",
"menuiconbg":"secondary-bg",
"menuicon":"fas fa-cloud",
"created_at":null,
"updated_at":null,
"subcategory":[
],
"categoryprop":[
]
},
{
"id":6,
"parrent_id":2,
"order":3,
"name":"S\u0131n\u0131rs\u0131z Hosting",
"slug":"sinirsiz-hosting",
"type":"hosting",
"status":"1",
"menu":"0",
"megamenu":"0",
"megamcol":0,
"megamrow":0,
"menuheadershow":"0",
"menufootershow":"0",
"menuiconbg":"twitter-bg",
"menuicon":"fas fa-infinity",
"created_at":null,
"updated_at":null,
"subcategory":[
],
"categoryprop":[
]
}
]
},
]我预期的结果是:
[
{
"id":1,
"parrent_id":null,
"order":0,
"name":"Home Page",
"slug":"\/",
"type":"home-page",
"status":"1",
"menu":"0",
"megamenu":"0",
"megamcol":0,
"megamrow":0,
"menuheadershow":"0",
"menufootershow":"0",
"menuiconbg":"",
"menuicon":"",
"created_at":null,
"updated_at":null,
"subcategory":[
],
"categoryprop":[
]
},
{
"id":2,
"parrent_id":null,
"order":1,
"name":"Hosting",
"slug":"hosting",
"type":"hosting",
"status":"1",
"menu":"0",
"megamenu":"1",
"megamcol":2,
"megamrow":4,
"menuheadershow":"1",
"menufootershow":"1",
"menuiconbg":null,
"menuicon":null,
"created_at":null,
"updated_at":null,
"subcategory":[
{
"id":3,
"parrent_id":2,
"order":0,
"name":"Web Hosting",
"slug":"web-hosting",
"type":"hosting",
"status":"1",
"menu":"0",
"megamenu":"0",
"megamcol":0,
"megamrow":0,
"menuheadershow":"0",
"menufootershow":"0",
"menuiconbg":"primary-bg",
"menuicon":"fas fa-server",
"created_at":null,
"updated_at":null,
"subcategory":[
{
"id":15,
"parrent_id":3,
"order":0,
"name":"Web Hosting",
"slug":"web-hosting",
"type":"hosting",
"status":"1",
"menu":"0",
"megamenu":"0",
"megamcol":0,
"megamrow":0,
"menuheadershow":"0",
"menufootershow":"0",
"menuiconbg":"primary-bg",
"menuicon":"fas fa-server",
"created_at":null,
"updated_at":null,
"subcategory":[
],
"categoryprop":[
{
"menudescription":[
{
"id":3,
"category_id":15,
"order":0,
"type":"menudescription",
"name":"Men\u00fc A\u00e7\u0131klamas\u0131",
"created_at":"2022-09-09T16:37:02.000000Z",
"updated_at":"2022-09-09T16:37:02.000000Z",
"categorycontent":[
{
"id":5,
"categoryprop_id":3,
"title":"Bu bir denemedir",
"content":"Bu bir deneme",
"image":null,
"lang_id":1,
"created_at":"2022-09-09T16:37:02.000000Z",
"updated_at":"2022-09-09T16:37:02.000000Z",
"lang":{
"id":1,
"name":"T\u00fcrk\u00e7e",
"symbol":"TR",
"code":"tr",
"default":1,
"created_at":null,
"updated_at":null
}
},
{
"id":6,
"categoryprop_id":3,
"title":"Bur bir deneme",
"content":"Bu bir deneme",
"image":null,
"lang_id":2,
"created_at":"2022-09-09T16:37:02.000000Z",
"updated_at":"2022-09-09T16:37:02.000000Z",
"lang":{
"id":2,
"name":"English",
"symbol":"EN",
"code":"en_us",
"default":0,
"created_at":null,
"updated_at":null
}
}
]
},
{
"id":4,
"category_id":3,
"order":0,
"type":"menudescription",
"name":"Men\u00fc A\u00e7\u0131klamas\u01312",
"created_at":"2022-09-09T16:37:02.000000Z",
"updated_at":"2022-09-09T16:37:02.000000Z",
"categorycontent":[
{
"id":7,
"categoryprop_id":4,
"title":"Bu bir denemedir",
"content":"Bu bir deneme",
"image":null,
"lang_id":1,
"created_at":"2022-09-09T16:37:02.000000Z",
"updated_at":"2022-09-09T16:37:02.000000Z",
"lang":{
"id":1,
"name":"T\u00fcrk\u00e7e",
"symbol":"TR",
"code":"tr",
"default":1,
"created_at":null,
"updated_at":null
}
},
{
"id":8,
"categoryprop_id":4,
"title":"Bur bir deneme",
"content":"Bu bir deneme",
"image":null,
"lang_id":2,
"created_at":"2022-09-09T16:37:02.000000Z",
"updated_at":"2022-09-09T16:37:02.000000Z",
"lang":{
"id":2,
"name":"English",
"symbol":"EN",
"code":"en_us",
"default":0,
"created_at":null,
"updated_at":null
}
}
]
}
]
}
]
}
],
"categoryprop":[
{
"menudescription":[
{
"id":1,
"category_id":3,
"order":0,
"type":"menudescription",
"name":"Men\u00fc A\u00e7\u0131klamas\u0131",
"created_at":"2022-09-09T16:37:02.000000Z",
"updated_at":"2022-09-09T16:37:02.000000Z",
"categorycontent":[
{
"id":1,
"categoryprop_id":1,
"title":"Bu bir denemedir",
"content":"Bu bir deneme",
"image":null,
"lang_id":1,
"created_at":"2022-09-09T16:37:02.000000Z",
"updated_at":"2022-09-09T16:37:02.000000Z",
"lang":{
"id":1,
"name":"T\u00fcrk\u00e7e",
"symbol":"TR",
"code":"tr",
"default":1,
"created_at":null,
"updated_at":null
}
},
{
"id":2,
"categoryprop_id":1,
"title":"Bur bir deneme",
"content":"Bu bir deneme",
"image":null,
"lang_id":2,
"created_at":"2022-09-09T16:37:02.000000Z",
"updated_at":"2022-09-09T16:37:02.000000Z",
"lang":{
"id":2,
"name":"English",
"symbol":"EN",
"code":"en_us",
"default":0,
"created_at":null,
"updated_at":null
}
}
]
},
{
"id":2,
"category_id":3,
"order":0,
"type":"menudescription",
"name":"Men\u00fc A\u00e7\u0131klamas\u01312",
"created_at":"2022-09-09T16:37:02.000000Z",
"updated_at":"2022-09-09T16:37:02.000000Z",
"categorycontent":[
{
"id":3,
"categoryprop_id":2,
"title":"Bu bir denemedir",
"content":"Bu bir deneme",
"image":null,
"lang_id":1,
"created_at":"2022-09-09T16:37:02.000000Z",
"updated_at":"2022-09-09T16:37:02.000000Z",
"lang":{
"id":1,
"name":"T\u00fcrk\u00e7e",
"symbol":"TR",
"code":"tr",
"default":1,
"created_at":null,
"updated_at":null
}
},
{
"id":4,
"categoryprop_id":2,
"title":"Bur bir deneme",
"content":"Bu bir deneme",
"image":null,
"lang_id":2,
"created_at":"2022-09-09T16:37:02.000000Z",
"updated_at":"2022-09-09T16:37:02.000000Z",
"lang":{
"id":2,
"name":"English",
"symbol":"EN",
"code":"en_us",
"default":0,
"created_at":null,
"updated_at":null
}
}
]
}
]
}
]
},
{
"id":4,
"parrent_id":2,
"order":1,
"name":"Wordpress Hosting",
"slug":"wordpress-hosting",
"type":"hosting",
"status":"1",
"menu":"0",
"megamenu":"0",
"megamcol":0,
"megamrow":0,
"menuheadershow":"0",
"menufootershow":"0",
"menuiconbg":"accent-bg",
"menuicon":"fas fa-box",
"created_at":null,
"updated_at":null,
"subcategory":[
],
"categoryprop":[
]
},
{
"id":5,
"parrent_id":2,
"order":2,
"name":"Linux Hosting",
"slug":"linux-hosting",
"type":"hosting",
"status":"1",
"menu":"0",
"megamenu":"0",
"megamcol":0,
"megamrow":0,
"menuheadershow":"0",
"menufootershow":"0",
"menuiconbg":"secondary-bg",
"menuicon":"fas fa-cloud",
"created_at":null,
"updated_at":null,
"subcategory":[
],
"categoryprop":[
]
},
{
"id":6,
"parrent_id":2,
"order":3,
"name":"S\u0131n\u0131rs\u0131z Hosting",
"slug":"sinirsiz-hosting",
"type":"hosting",
"status":"1",
"menu":"0",
"megamenu":"0",
"megamcol":0,
"megamrow":0,
"menuheadershow":"0",
"menufootershow":"0",
"menuiconbg":"twitter-bg",
"menuicon":"fas fa-infinity",
"created_at":null,
"updated_at":null,
"subcategory":[
],
"categoryprop":[
]
}
]
},
{
"id":11,
"parrent_id":null,
"order":1,
"name":"Alan Ad\u0131",
"slug":"alanadi",
"type":"alanadi",
"status":"1",
"menu":"0",
"megamenu":"0",
"megamcol":0,
"megamrow":0,
"menuheadershow":"0",
"menufootershow":"0",
"menuiconbg":null,
"menuicon":null,
"created_at":null,
"updated_at":null,
"subcategory":[
],
"categoryprop":[
]
}
]发布于 2022-09-21 19:08:05
首先,数组太复杂了。我觉得你应该简化一下。
我有两种解决办法。这些是Laravel方法和纯PHP方法。
方法-1 (Laravel)
在开始此操作之前,必须确保"subcategory和categoryprop“的值是Illuminate\Support\Collection的实例。如果它们不是Collection对象的实例,请使用方法-2。
class Method1
{
public function handle() {
// ...
var_dump($categories->map($this, 'mapper'));
}
public function mapper($collection) {
// If it has sub-categories, map them
if (optional($collection['subcategory'])->count()) {
$collection['subcategory'] = $collection['subcategory']->map([$this, 'mapper']);
}
$lastItemIndex = $collection->count() - 1;
// If it is an array of categories, map them.
if (isset($collection[$lastItemIndex])) {
return $collection->map([$this, 'mapper']);
}
// If there is no item in $collection['categoryprop']
if (0 >= optional($collection->get('categoryprop'))->count()) {
return $collection;
}
$collection['categoryprop'] = $collection['categoryprop']->groupBy('type');
return $collection;
}
}方法-2(纯PHP)
class Method2
{
public function handle() {
// ...
var_dump($this->groupByCategoryProp($categories));
}
public function groupByItemKey($collection, $key) {
$generatedList = [];
foreach ($collection as $item) {
$type = $item[$key];
$generatedList[$type][] = $item;
}
return $generatedList;
}
public function groupByCategoryProp($collection) {
// If it has sub-categories, map them.
if (isset($collection['subcategory']) && 0 < count($collection['subcategory']) ) {
$subCategory = $this->groupByCategoryProp($collection['subcategory']);
$collection['subcategory'] = $subCategory;
}
$lastItemIndex = count($collection) - 1;
// If it is an array of categories, map them.
if (isset($collection[$lastItemIndex])) {
foreach ($collection as $i => $category) {
$collection[$i] = $this->groupByCategoryProp($category);
}
return $collection;
}
// If there is no categoryprop
if (! isset($collection['categoryprop'])) {
return $collection;
}
// If there is no item in $collection['categoryprop']
if (0 === count($collection['categoryprop']) ) {
return $collection;
}
$collection['categoryprop'] = $this->groupByItemKey($collection['categoryprop'], 'type') ;
return $collection;
}
}https://stackoverflow.com/questions/73786978
复制相似问题