group array by it's attribute

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
User avatar
ahmed31916
Advanced
Posts: 148
Joined: 14 Apr 2022, 12:15

group array by it's attribute

Post by ahmed31916 » 20 Jun 2022, 07:39

Hello Aimeos,

For this array, how can I group it by it's attribute?

Code: Select all

 array:35 [▼
  61 => Aimeos\MShop\Product\Item\Standard {#494 ▼
    -available: true
    -modified: false
    -prefix: "product."
    #bdata: array:20 [▶]
    -date: "2022-06-20 07:37:00"
    -listItems: array:1 [▶]
    -listRefItems: array:1 [▼
      "attribute" => array:1 [▼
        21 => Aimeos\MShop\Attribute\Item\Standard {#541 ▶}
      ]
    ]
    -listRmItems: []
    -listPrepared: false
    -listRmMap: []
    -listMap: []
    -listMax: 1
    -propItems: []
    -propRmItems: []
    -propMax: 0
  }
  83 => Aimeos\MShop\Product\Item\Standard {#461 ▶}
  62 => Aimeos\MShop\Product\Item\Standard {#464 ▶}
  84 => Aimeos\MShop\Product\Item\Standard {#545 ▶}
  63 => Aimeos\MShop\Product\Item\Standard {#465 ▶}

User avatar
aimeos
Administrator
Posts: 7866
Joined: 01 Jan 1970, 00:00

Re: group array by it's attribute

Post by aimeos » 21 Jun 2022, 12:50

You can do something like this:

Code: Select all

$map = $products->getRefItems( 'attribute' )
	->flat( 1 )
	->getCode()
	->flip()
	->map( function( $val, $key ) use ( $products ) {
		return $products->get( $val );
	} );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply