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
            )
    )
3
  • 1
    check this out stackoverflow.com/questions/6785355/… Commented Jun 11, 2019 at 11:03
  • 1
    Possible duplicate of Convert multidimensional array into single array Commented Jun 11, 2019 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. Commented Jun 11, 2019 at 11:15

1 Answer 1

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;
            }   
        }
Sign up to request clarification or add additional context in comments.

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.