Search code examples
phpmorris.js

PHP Forming Custom Array from Array within array


I'm having an array of three months which has Three Months Data and it holds Savings & Discount Information something like the below:

graphData holds the DataArr.

[DataArr] => Array

    [0] => Array
            (
             [data] => Array
                 (
                   [0] => Array 
                      ( 
                        [Month] => 10 Jan 2019
                        [Cost] => 60.3
                        [Name] => Savings
                      )
                   [1] => Array 
                      ( 
                        [Month] => 10 Feb 2019
                        [Cost] => 45.3
                        [Name] => Savings
                      )
                   [2] => Array 
                      ( 
                        [Month] => 10 Mar 2019
                        [Cost] => 50.6
                        [Name] => Savings
                      )
                )
           )
    [1] => Array
            (
             [data] => Array
                 (
                   [0] => Array 
                      ( 
                        [Month] => 10 Jan 2019
                        [Cost] => 89.62
                        [Name] => Discount
                      )
                   [1] => Array 
                      ( 
                        [Month] => 10 Feb 2019
                        [Cost] => 20.2
                        [Name] => Discount
                      )
                   [2] => Array 
                      ( 
                        [Month] => 10 Mar 2019
                        [Cost] => 0.0
                        [Name] => Discount
                      )
                )
           )

REQUIRED ARRAY - To Pass into Morris.js Line Chart

Now I want to iterate into this array and want to have the array format in a way as below

           [data] => Array
                 (
                   [0] => Array 
                      ( 
                        [Month] => 10 Jan 2019
                        [Savings] => 60.3
                        [Discount] => 89.62

                      )
                   [1] => Array 
                      ( 
                        [Month] => 10 Feb 2019
                        [Saving] => 45.3
                        [Discount] => 20.2
                      )
                   [2] => Array 
                      ( 
                        [Month] => 10 Mar 2019
                        [Saving] => 50.6
                        [Discount] => 0.0
                      )
                )

Can you please share me how I can go about getting this into the required array using PHP? I tried array_push and other methods with JQuery Array methods as well, but all my efforts failed, and I'm working on this from past 3 days, any help is greatly appreciated.

Even tried with PHP foreach but even that failed, missing something and have no more ideas on how to get this working! Please let me know if any of you have much ideas on this.

Thanks

EDIT

**SAMPLE CODE I TRIED IN PHP **

for($k = 0; $k < sizeof($graphData['DataArr']); $k++ ) {
                for($l = 0; $l < sizeof($graphData['DataArr'][$k]); $l++ ) {
                    array_push($graphData['DataArr'], array(
                        'label' => $graphData['DataArr'][$k][$l]['label'], 
                        'percent' => $graphData['DataArr'][$k][$l]['percentage']
                            )
                    );
                }
            }

Here label => COST, DISCOUNT and Percentage is the value associated with the labels.


Solution

  • <?php
    
    $graphData =
        array(
            'DataArr' => 
            array
                (
                array(
                 'data' => array
                     (
                       array 
                          ( 
                            'Month' => '10 Jan 2019',
                            'Cost' => 60.3,
                            'Name' => 'Savings'
                          ),
                       array 
                          ( 
                            'Month' => '10 Feb 2019',
                            'Cost' => 45.3,
                            'Name' => 'Savings'
                          ),
                       array 
                          ( 
                            'Month' => '10 Mar 2019',
                            'Cost' => 50.6,
                            'Name' => 'Savings'
                          )
                    )
                ),
                array
                (
                 'data' => array
                     (
                       array 
                          ( 
                            'Month' => '10 Jan 2019',
                            'Cost' => 89.62,
                            'Name' => 'Discount'
                          ),
                       array 
                          ( 
                            'Month' => '10 Feb 2019',
                            'Cost' => 20.2,
                            'Name' => 'Discount'
                          ),
                       array 
                          ( 
                            'Month' => '10 Mar 2019',
                            'Cost' => 0.0,
                            'Name' => 'Discount'
                        )
                    )
               )
        )
    );
    
    
    $result = [];
    
    foreach($graphData['DataArr'] as $value){
        foreach($value['data'] as $details){
            if(!isset($result['data'][$details['Month']])) $result['data'][$details['Month']]  = ['Month' => $details['Month']];
            $result['data'][$details['Month']][$details['Name']] = $details['Cost'];
        }
    }
    
    $result['data'] = array_values($result['data']); // to eliminate keys
    print_r($result);
    

    Demo: https://3v4l.org/QMVuR

    • We first create a $result array with data key in it.
    • We loop over $graphData and store the values of Month,Savings and Discount under the indexed key value of Month.
    • In the end, we do array_values() to remove the keys.