1

I have to modify the original data into a new set of data using PHP, The original from query would return like :

Result                                   Expected Result :
[                                        [
 {                                        {
  'PRODUCT' : 'BANANA',                    'PRODUCT' : 'BANANA',
  'BUY_PERIOD' : '202101',                 'BUY_PERIOD' : '202101',
  'SALES_PERIOD' : '202101',               'SALES_PERIOD' : [ 
  'PERCENT_OF_SALES' : "20",                                 {
 },                                                            '202101' : "20",
 {                                                             '202102' : "10",
  'PRODUCT' : 'BANANA',                                        '202103' : "15",
  'BUY_PERIOD' : '202101',                                     '202104' : "35"
  'SALES_PERIOD' : '202102',                                 }
  'PERCENT_OF_SALES' : "10",                                ]
 },                                       }
 {                                       ]
  'PRODUCT' : 'BANANA',
  'BUY_PERIOD' : '202101',
  'SALES_PERIOD' : '202103',
  'PERCENT_OF_SALES' : "15",
 },
 {
  'PRODUCT' : 'BANANA',
  'BUY_PERIOD' : '202101',
  'SALES_PERIOD' : '202104',
  'PERCENT_OF_SALES' : "35",
 },
]

I need the expected result to make something like : enter image description here

On my previous question, i already asked the similiar question, using JavaScript ES6.Here is the the link to my previous question JSON Multidimensional Array to HTML Table
This time, i had to do it using PHP (Laravel)
Any kind of help will be really appriciated , Thank you

3
  • Does this answer your question? PHP JSON Array - Group by the same value or PHP JSON Group by the same value Commented Jun 24, 2021 at 7:13
  • @DefinitelynotRafal i have tried the first code earlier, the second code almost work, but still working on modifying it. the result still not as expected Commented Jun 24, 2021 at 7:26
  • 2
    Then you should be able to show us what you tried, which are you expected to do. That way we can improve on your attempt and help you understand what went wrong. Commented Jun 24, 2021 at 7:57

2 Answers 2

1

You can use array_reduce function as approach:

$result = array_reduce(
    $data,
    function($res, $el) {
        if (isset($res[$el['PRODUCT'] . "-" . $el['BUY_PERIOD']])) {
            $res[$el['PRODUCT'] . "-" . $el['BUY_PERIOD']]['SALES_PERIOD'][$el['SALES_PERIOD']] = $el['PERCENT_OF_SALES'];
        } else {
            $res[$el['PRODUCT'] . "-" . $el['BUY_PERIOD']] = [
                'PRODUCT' => $el['PRODUCT'],
                'BUY_PERIOD' => $el['BUY_PERIOD'],
                'SALES_PERIOD' => [
                    $el['SALES_PERIOD'] => $el['PERCENT_OF_SALES']
                ]
            ];
        }
        
        return $res;
    },
    
    $res
);

PHP execute online

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

3 Comments

This works well, but if i add more buy_period data , for example 202102 , the new buy period didn't show up
Here I added new buy period. It looks ok: phpize.online/…
thank you sir, this allows me to continue to make a pivot table using new dataset
0

This doesn't return the data exactly in the format you require, but gives you the same nested structure using two keys as the question specifies. Use PRODUCT as the first key in the array, then SALES_PERIOD as a second key. Add each row to the nested array through your foreach

foreach($result as $row){
    $return[$row['PRODUCT']]['PRODUCT'] = $result['PRODUCT'];
    $return[$row['PRODUCT']]['BUY_PERIOD'] = $result['BUY_PERIOD'];
    $return[$row['PRODUCT']]['SALES_PERIOD'][$result['SALES_PERIOD']] = $result['PERCENT_OF_SALES'];
}

json_encode($return);

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.