0

I have following array output.i want to combine them according to tax class id Please check the below array and another array format which i want is 2nd array

[0] => Array
                (
                    [id] => 1947
                    [cat_id] => 48
                    [tax_class_id] => 18
                    [sku] => 620068-429-S
                    [qty_ordered] => 5
                )

            [1] => Array
                (
                    [id] => 1947
                    [cat_id] => 48
                    [tax_class_id] => 28
                    [sku] => 620068-429-M
                    [qty_ordered] => 9
                )

            [2] => Array
                (
                    [id] => 1947
                    [cat_id] => 48
                    [tax_class_id] => 18
                    [sku] => 620068-429-L
                    [qty_ordered] => 9
                )

            [3] => Array
                (
                    [id] => 1947
                    [cat_id] => 48
                    [tax_class_id] => 28
                    [sku] => 620068-429-XL
                    [qty_ordered] => 9
                )

I want to combine array those have same tax_class_id like below

[0] => Array(
                        [0] => Array
                        (
                            [id] => 1947
                            [cat_id] => 48
                            [tax_class_id] => 18
                            [sku] => 620068-429-S
                            [qty_ordered] => 5
                        )
                        [1] => Array
                        (
                            [id] => 1947
                            [cat_id] => 48
                            [tax_class_id] => 18
                            [sku] => 620068-429-L
                            [qty_ordered] => 9
                        )

                )
                [1] => Array(

                     [0] => Array
                        (
                            [id] => 1947
                            [cat_id] => 48
                            [tax_class_id] => 28
                            [sku] => 620068-429-M
                            [qty_ordered] => 9
                        )

                    [1] => Array
                        (
                            [id] => 1947
                            [cat_id] => 48
                            [tax_class_id] => 28
                            [sku] => 620068-429-XL
                            [qty_ordered] => 9
                        )
                )

How can i get array in above format. where sub array has same tax_class_id.

Karan Adhikari
  • 60
  • 1
  • 2
  • 9

3 Answers3

0

You can do a loop like this:

    $val = [];
    $newArray = []
    foreach ($products as $product) {

      $key = array_search($product['tax_class_id'],$val);
      if(!$key) {
        $val[] = $product['tax_class_id'];
        $key = array_search($product['tax_class_id'],$val);
      }
      $newArray[$key][] = $product;
    }

demo:https://ideone.com/OhRieF#stdin

madalinivascu
  • 32,064
  • 4
  • 39
  • 55
0

You could do a combination of "usort()" and creating a new array via loop from the resulting usort() array.

usort() example to get you started (based on Sort multi-dimensional array by specific key )

<!DOCTYPE html>
<html>
<body>

<?php
$age = array(
    array(
    "id"=>"1", 
    "cat_id"=>"11", 
    "text_id"=>"43"
    ),
    array(
    "id"=>"2", 
    "cat_id"=>"22", 
    "text_id"=>"22"
    ),
    array(
    "id"=>"3", 
    "cat_id"=>"33", 
    "text_id"=>"43"
    ),
    array(
    "id"=>"4", 
    "cat_id"=>"44", 
    "text_id"=>"43"
    ),
    array(
    "id"=>"5", 
    "cat_id"=>"55" ,
    "text_id"=>"22"
    )

);

 function cmp($a, $b)
 {
     return strcmp($a['text_id'], $b['text_id']);
 }

 usort($age, "cmp");

// Just to show that the array has been sorted
echo '<pre>';
print_r($age);
echo '</pre>';

// Create new array using loops here
//......

?>

</body>
</html>
Nax.S
  • 92
  • 2
  • 11
0

Solution....

foreach($array as $row){
    $new[$row['tax_class_id']][] = $row;
}
echo "<pre>";print_r($new);

Result

Array
(
[18] => Array
    (
        [0] => Array
            (
                [id] => 1947
                [cat_id] => 48
                [tax_class_id] => 18
                [sku] => 620068-429-S
                [qty_ordered] => 5
            )

        [1] => Array
            (
                [id] => 1947
                [cat_id] => 48
                [tax_class_id] => 18
                [sku] => 620068-429-L
                [qty_ordered] => 9
            )

    )

[28] => Array
    (
        [0] => Array
            (
                [id] => 1947
                [cat_id] => 48
                [tax_class_id] => 28
                [sku] => 620068-429-M
                [qty_ordered] => 9
            )

        [1] => Array
            (
                [id] => 1947
                [cat_id] => 48
                [tax_class_id] => 28
                [sku] => 620068-429-XL
                [qty_ordered] => 9
            )

    )

)
GYaN
  • 2,327
  • 4
  • 19
  • 39