Laravel get group of arrays

laravel group by
laravel collection group by
laravel collection group by multiple
laravel collection to array of objects
php array group by
laravel collection group by count
laravel group by relationship
laravel get collection from collection

I have code below and result is like:

array:2 [▼
  0 => array:1 [▼
    "CPU" => "AMD a5"
  ]
  1 => array:1 [▼
    "CPU" => "AMD a9"
  ]
]

I want to group them in same array like

CPU => [
"0" => "AMD a5",
"1" => "AMD a9"
]
Code
$category = Category::where('slug', $slug)->where('status', '=', 'active')->first();
$products = $category->products;
    foreach ($products as $product) {
        foreach($product->attributes as $attribut){
            $attributes[] = [$attribut->group->title => $attribut->title];
        }
    }

What should I change?

If you want a bit of a more scalable solution where you might have multiple attribute types, the following may help:

$attributes = [
    [
        'CPU' => 'AMD A5'
    ],
    [
        'CPU' => 'AMD A9'
    ],
    [
        'GFX' => 'AMD'
    ]  
];

return collect($attributes)
    ->groupBy(function($item) {
        return key($item);
    })
    ->map(function($item) {
        return array_flatten($item); 
    })
    ->toArray();

The output of the above will be:

array:2 [▼
  "CPU" => array:2 [▼
    0 => "AMD A5"
    1 => "AMD A9"
  ]
  "GFX" => array:1 [▼
    0 => "AMD"
  ]
]

Here's an example you can play with.

Grouping array results, Hey Ok, so I've got a two-dimensional array which has two keys: name and email. GroupBy https://laravel.com/docs/5.2/collections#available-methods I want to send a list of all the names that get a hit in one email to the� {tip} If you need to sort a collection of nested arrays or objects, see the sortBy and sortByDesc methods. sortBy() The sortBy method sorts the collection by the given key. The sorted collection keeps the original array keys, so in this example we'll use the values method to reset the keys to consecutively numbered indexes:

if your $attribut->group->title is "CPU" and your $attribut->title is "AMD a5"

then you can use this

foreach ($products as $product) {
    foreach($product->attributes as $attribut){
        $attributes[$attribut->group->title][] =  $attribut->title;
    }
}

from @mozammil answer if you still wanna do using foreach

$attributes =
    [
        [
            'CPU' => 'AMD A5'
        ],
        [
            'CPU' => 'AMD A9'
        ],
        [
            'GFX' => 'AMD'
        ]
    ];
$data = [];
foreach ($attributes as $titles){
    foreach ($titles as $title=>  $row){
        $data[$title][] = $row;
    }
}

Collections - Laravel, Since Laravel v5.5.29 you can group collections by multiple levels. We are passing an array instead of a string to define multiple levels. First the students should be grouped by the skilllevel and then by the teacher. So this is what we get: The head is a simple wrapper around PHP's reset function. This function will return the first element of an array or false if the array is empty. This function does not modify the array.

You can try collection methods here is an example.

$keyed = $collection->map(function ($item,$key) {
    return [[$key] => $item['CPU']];
});

For more information

https://laravel.com/docs/5.7/collections#method-map

GroupBy multiple levels in Laravel, Laravel 5 Collections: Grouping Collection Elements With groupBy The groupBy method is used to group a collection based on a given value, object( Illuminate\Support\Collection) protected 'items' => array 0 => array� I'm trying to sort an array of objects, by a property on the objects. The data structure looks like this: Array ( [0] => stdClass Object

Use array_column to get one column of an array.

$category = Category::where('slug', $slug)->where('status', '=', 'active')->first();
$products = $category->products;
$cpu = array_column($products, "CPU");

This will return an array as your expected result.

https://3v4l.org/UYPhK

This requires PHP 7 since in older versions array_column can't handle objects

Laravel 5 Collections: Grouping Collection Elements With groupBy , June 22nd 2018; 45K; 5 Comments For example, in PHP it's possible to group an associative array by some key, so you will be able It iterates once, creating a new key for the new array with the value of the data with the specified Which PHP Framework to Choose: Laravel vs CodeIgniter vs Symfony. Laravel includes a variety of global "helper" PHP functions. Many of these functions are used by the framework itself; however, you are free to use them in your own applications if you find them convenient. Available Methods Arrays & Objects

How to group an array of associative arrays by key in PHP, Parameters. [ ]: It is an array passed to the group method as a first parameter. Example of Route Groups. web.php. Laravel's database query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database operations in your application and works on all supported database systems. The Laravel query builder uses PDO parameter binding to protect your application against SQL injection attacks.

Laravel Route Groups, Now, let's get to today's topic – how to group the data. Here's the Function groupBy() is called AFTER the get(), it is a Collection function and not Eloquent. If you do But when I echo the variable it show under array. Reply. For example, you could check a user group, or some other form of access and deny it if they failed to be a part of the group. The important area to take notice of is the array key. Laravel

Group Entries By Day: Collection groupBy() With , (PHP 4 >= 4.2.0, PHP 5, PHP 7) Returns a multidimensional numerically indexed array, starting with zero, with each A breakdown by groups with excess: Laravel collections are one of the most powerful provisions of the Laravel framework. They are what PHP arrays should be, but better. Laravel collections are what PHP arrays should be, but better. In this tutorial, we will be going through a few tricks that may be handy when you are working with collections.

Comments
  • What if $attributes[] = $attribut->group->title => $attribut->title;?
  • Exactly the kind of results i want, i will try this and let you know thanks
  • Sounds good. You can also chain a toArray() if you want the results to be an array.
  • Just to know, if i want try array function where should i add toArray?
  • this does work so is the Kelvin answer first part.
  • thank you, your first part does work for me just as Mozammil answer
  • You mean like [$attribut->group->title => array($attribut->title)]?
  • No just as the code is written. No loops at all. Edited the answer
  • I don't get it, my CPU is dynamic name is not static. my Cpu = $attribute->group->title
  • In that case your question is unclear. Edit your question to make it more clear
  • I get what is the point of your solution but I'm afraid it isn't what I'm looking for, what I'm trying to do here is when I'm visiting category page get all attributes under that category products so nothing static here as cpu etc.