0

I have an array of arrays, and I want to filter that array by multiple key values, and group the arrays with matching keys if there are any. Example array:

Array
(
    [0] => Array
        (
            [id] => 1
            [value] => 11
            [quantity] => 14
        )

    [1] => Array
        (
            [id] => 2
            [value] => 11
            [quantity] => 14
        )

    [2] => Array
        (
            [id] => 3
            [value] => 22
            [quantity] => 14
        )

    [3] => Array
        (
            [id] => 4
            [value] => 22
            [quantity] => 14
        )
    [4] => Array
        (
            [id] => 5
            [value] => 23
            [quantity] => 15
        )
)

and let's say I want the arrays with matching value and quantity to be grouped in a new array

The desired output would be something like this:

Array
(
    [11] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [value] => 11
                    [quantity] => 14
                )

            [1] => Array
                (
                    [id] => 2
                    [value] => 11
                    [quantity] => 14
                )
        )
    [22] => Array
        (
            [0] => Array
                (
                    [id] => 3
                    [value] => 22
                    [quantity] => 14
                )
            [1] => Array
                (
                    [id] => 4
                    [value] => 22
                    [quantity] => 14
                )
        )

    [23] => Array
        (
            [0] => Array
                (
                    [id] => 5
                    [value] => 23
                    [quantity] => 15
                )
        )
)

I'm clueless on how to achieve this.

1
  • You couldn't even try some code using a simple foreach loop? Commented Dec 5, 2018 at 18:02

4 Answers 4

1

A simple foreach loop over your array to ceate a new array will suffice for this purpose

$new_arr = [];

foreach ($inArray as $arr ) {
    $new_arr[$arr['value']][] = $arr;
}

// unset the original array if you are finished with it
// in case it is large and you could make better use
// of the memory for something else
unset($inArray);
Sign up to request clarification or add additional context in comments.

2 Comments

That works if I only want to group them by matching value. In my case I want to be able to group them by let's say both value and quantity, or even more.
Well then first you have to tell use what you want to group them by. Then you have to tell us what you want the new arrays key to be.
1

If you want to group by the values of multiple keys of the inner arrays, you can join those values together to form the key in your result array.

foreach ($array as $item) {
    // combine value and quantity, for example
    $key = $item['value'] . '|' . $item['quantity'];
    $result[$key][] = $item;
}

Comments

0

just pass an array References and a sort key

function sortBy(&$arr,$by){
 $result=array();
 foreach($arr as $value){
  if(isset($value[$by]))
   $result[$value[$by]][]=$value;
  }
  return $result;
 }

Examples

$sorted=sortBy($yourArray,'value'); //by value
$sorted=sortBy($yourArray,'id'); //by Idx
$sorted=sortBy($yourArray,'quantity'); //quantity

3 Comments

I want to be able to sort by BOTH value AND quantity at the same time. This is what's difficult for me.
either you instilled a bad example or your data structure isn't correct. can you explain more please
yeah sorry the example is bad. well basically what I want to do is group the arrays that have matching value AND quantity into a new array, and the ones that don't have matching value and quantity into separate arrays. from the example input: the first and second arrays should be into a new array the third and fourth arrays into a new array and the fifth alone into a new array I hope this explained it better
0

You want to group the array keys passing, if I understood correctly.

I usually use the laravel collection library, because it's provided out of the box. ALthoug, here's my contribution.

Let's try:

function groupArray( $array, $key, $remove = null )
{
    $result = array();
    foreach (array_unique(array_column($array, $key)) as $value) {
        $result[$value] = array_map(function ( $item ) use ( $remove ) {
            unset($item[$remove]);
            return $item;
        }, array_filter($array, function ( $item ) use ( $value, $key ) {
            return $item[$key] === $value;
        }));
    }
    return $result;
}

The above function does the job, first we get all the selected key values using the array_column function. THen we do a foreach in the array to filter the array data using the provided key and finally we remove the selected key, if necessary (just because the selected key will be the grouped array keys).

Usage:

    $sample = array(
    [
        'id' => 1,
        'value' => 11,
        'quantity' => 14
    ],
    [
        'id' => 2,
        'value' => 11,
        'quantity' => 14
    ],
    [
        'id' => 3,
        'value' => 22,
        'quantity' => 14
    ],
    [
        'id' => 4,
        'value' => 22,
        'quantity' => 14
    ],
    [
        'id' => 5,
        'value' => 23,
        'quantity' => 14
    ],
);
$a = groupArray($sample, 'value', 'value');
$b = groupArray($sample, 'value');
$c = groupArray($sample, 'quantity');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.