Page 1 of 1

group array by it's attribute

Posted: 20 Jun 2022, 07:39
by ahmed31916
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 ▶}

Re: group array by it's attribute

Posted: 21 Jun 2022, 12:50
by aimeos
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 );
	} );