0

I am trying to get the product value based on a product in the multidimensional array. please help me to get the result.

I have one multidimensional array and this array I have to convert into a single array with sort. I tried to use call_user_func_array('array_merge', $ranges); this function. After using this function get a single array.

    Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [product_name] => 44 kg LPG
                            [value] => 0
                        )

                    [1] => Array
                        (
                            [id] => 2
                            [product_name] => 22 Kg LPG
                            [value] => 0
                        )
                )
            [1] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [product_name] => 44 kg LPG
                            [value] => 2
                        )

                    [1] => Array
                        (
                            [id] => 2
                            [product_name] => 22 Kg LPG
                            [value] => 3
                        )
                )
        )

I am expecting this result.

    Array
    (
        [0] => Array
            (
                [0] => 44 kg LPG
                [1] => 0
                [2] => 2
            )
        [1] => Array
            (
                [0] => 22 Kg LPG
                [1] => 0
                [2] => 3
            )
    )
04FS
  • 5,660
  • 2
  • 10
  • 21
AngularJMK
  • 1,178
  • 13
  • 15
  • 1
    check this out https://stackoverflow.com/questions/6785355/convert-multidimensional-array-into-single-array – TheLastStark Jun 11 '19 at 11:03
  • 1
    Possible duplicate of [Convert multidimensional array into single array](https://stackoverflow.com/questions/6785355/convert-multidimensional-array-into-single-array) – Andrei Lupuleasa Jun 11 '19 at 11:05
  • 1
    IMHO not a use case for `array_merge` or anything like that to begin with - but for a purposefully written own little function, that simply loops over the input array, and creates the structure you want from it. – 04FS Jun 11 '19 at 11:15

1 Answers1

0

Finally, I am getting the output using this method...

$result = array();

        if (COUNT($reportData) > 0) {
            $count = COUNT($reportData);


            foreach ($reportData as $key => $value) {
                $data = array();
                $data[0] = $value[$key]['product_name'];

                for ($i=0; $i <= ($count-1); $i++) {
                    $data[$i+1] = $reportData[$i][$key]['value'];
                }

                $result[] = $data;
            }   
        }
AngularJMK
  • 1,178
  • 13
  • 15