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.

2
  • 1
    you use your head and you do a loop Commented Oct 17, 2017 at 8:15
  • @madalinivascu ha ha thanks brother Commented Oct 17, 2017 at 8:17

3 Answers 3

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
            )

    )

)
Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.