Home > database >  Filter and re-format a given multi-dimensional array (php)
Filter and re-format a given multi-dimensional array (php)

Time:03-16

I have been given a dataset by my tutor and asked to carry out the following:

// make a function
// get skus of which the figure of the average monthly sales unit is equal or greater than 350
// array in the following format:
[
'id' => 11102,                              
    'sku' => 'TEST_3',
    'alternates' => [
        'asin' => [
            'code' => '50',
            'value' => 'JL1235',
        ],
        'barcode' => [
            'code' => '10',
            'value' => 'JS160694',
        ],
    ],
    'commodityCode' => '9989898889',
    'price' => [
        'value' => 145.99,
    ],
    'instructions' => [
        'picking' => [
            'code' => 'PICK',
            'value' => 'I\'VE JUST HAD AN UNHAPPY LOVE AFFAIR, SO I DON\'T SEE WHY ANYBODY ELSE SHOULD HAVE A GOOD TIME.',
        ],
    ],
    'quantity' => [
        'inner' => 100,
        'masterCarton' => 200,
        'pallet' => 300,
    ],
    'averageMonthlyUnitSales' => 100,
    'created_at' => '2022-03-13 04:14:12'
],

Example dataset is here:

$data = [
    [
        'id' => '9947',
        'sku' => 'test_1',
        'asin_code' => '50',
        'asin_value' => '',
        'barcode' => '10',
        'barcode_value' => 'js160694',
        'commodityCode' => '9989898889',
        'price' => '120.50',
        'picking_instruction_code' => 'PICK',
        'picking_instruction_value' => '',
        'qty_inner' => '120',
        'qty_masterCarton' => '200',
        'qty_pallet' => '1200',
        'averageMonthlyUnitSales' => '750',
        'created_at' => '2019-02-23T01:54:14.957299 00:00'
    ],
    [
        'id' => '10921',
        'sku' => 'test_2',
        'asin_code' => '50',
        'asin_value' => 'bx12345',
        'barcode' => '10',
        'barcode_value' => 'jb170931',
        'commodityCode' => '9989898889',
        'price' => '20.59',
        'picking_instruction_code' => 'PICK',
        'picking_instruction_value' => 'It\'s only half completed, I\'m afraid',
        'qty_inner' => '70',
        'qty_masterCarton' => '250',
        'qty_pallet' => '270',
        'averageMonthlyUnitSales' => '120',
        'created_at' => '2021-12-23T11:41:31.193982 00:00'
    ],
    [
        'id' => '11102',
        'sku' => 'test_3',
        'asin_code' => '50',
        'asin_value' => 'jl1235',
        'barcode' => '10',
        'barcode_value' => 'js160694',
        'commodityCode' => '9989898889',
        'price' => '145.99',
        'picking_instruction_code' => 'PICK',
        'picking_instruction_value' => 'I\'ve just had an unhappy love affair, so I don\'t see why anybody else should have a good time.',
        'qty_inner' => '100',
        'qty_masterCarton' => '200',
        'qty_pallet' => '300',
        'averageMonthlyUnitSales' => '100',
        'created_at' => '2022-03-13T04:14:12.11.093745  00:00'
    ],
    
];

I can perform the first part of the assignment (filter for averageMonthlyUnitSales>350) by carrying out something like:

$filtered_array= array_filter($data, function($item){
return ($item['averageMonthlyUnitSales']>350);
});

But I am not too sure how I can go about getting a new array in the required format?

CodePudding user response:

The only way I can see to simply do both is as follows.

$data = [
    [
        'id' => '9947',
        'sku' => 'test_1',
        'asin_code' => '50',
        'asin_value' => '',
        'barcode' => '10',
        'barcode_value' => 'js160694',
        'commodityCode' => '9989898889',
        'price' => '120.50',
        'picking_instruction_code' => 'PICK',
        'picking_instruction_value' => '',
        'qty_inner' => '120',
        'qty_masterCarton' => '200',
        'qty_pallet' => '1200',
        'averageMonthlyUnitSales' => '750',
        'created_at' => '2019-02-23T01:54:14.957299 00:00'
    ],
    [
        'id' => '10921',
        'sku' => 'test_2',
        'asin_code' => '50',
        'asin_value' => 'bx12345',
        'barcode' => '10',
        'barcode_value' => 'jb170931',
        'commodityCode' => '9989898889',
        'price' => '20.59',
        'picking_instruction_code' => 'PICK',
        'picking_instruction_value' => 'It\'s only half completed, I\'m afraid',
        'qty_inner' => '70',
        'qty_masterCarton' => '250',
        'qty_pallet' => '270',
        'averageMonthlyUnitSales' => '120',
        'created_at' => '2021-12-23T11:41:31.193982 00:00'
    ],
    [
        'id' => '11102',
        'sku' => 'test_3',
        'asin_code' => '50',
        'asin_value' => 'jl1235',
        'barcode' => '10',
        'barcode_value' => 'js160694',
        'commodityCode' => '9989898889',
        'price' => '145.99',
        'picking_instruction_code' => 'PICK',
        'picking_instruction_value' => 'I\'ve just had an unhappy love affair, so I don\'t see why anybody else should have a good time.',
        'qty_inner' => '100',
        'qty_masterCarton' => '200',
        'qty_pallet' => '300',
        'averageMonthlyUnitSales' => '100',
        'created_at' => '2022-03-13T04:14:12.11.093745  00:00'
    ],
    
];

$new = [];
foreach ($data as $line){
    if ( $line['averageMonthlyUnitSales'] > 350 ){
        // reformat the array
        $new = [
                'id' => $line['id'], 
                'sku' => $line['sku'],
                'alternates' => ['asin' => ['code'=>$line['asin_code'], 'value'=>$line['asin_value'] ],
                                'barcode' => ['code'=> $line['barcode'], 'value' => $line['barcode_value']]
                                ],
                'commodityCode' => $line['commodityCode'],
                'price' => [ 'value' => $line['price'] ],
                'instructions' => ['picking' => ['code' => $line['picking_instruction_code'], 'value'=> $line['picking_instruction_value']]
                            ],
                'quantity' =>['inner' =>$line['qty_inner'], 'masterCarton' =>$line['qty_masterCarton'], 'pallet'=>$line['qty_pallet']],
                'averageMonthlyUnitSales'=>$line['averageMonthlyUnitSales'],
                'created_at'=>$line['averageMonthlyUnitSales']
        ];
                            
    }
}
print_r($new);

RESULT

PHP 8.1.3
Array
(
    [id] => 9947
    [sku] => test_1
    [alternates] => Array
        (
            [asin] => Array
                (
                    [code] => 50
                    [value] => 
                )

            [barcode] => Array
                (
                    [code] => 10
                    [value] => js160694
                )

        )

    [commodityCode] => 9989898889
    [price] => Array
        (
            [value] => 120.50
        )

    [instructions] => Array
        (
            [picking] => Array
                (
                    [code] => PICK
                    [value] => 
                )

        )

    [quantity] => Array
        (
            [inner] => 120
            [masterCarton] => 200
            [pallet] => 1200
        )

    [averageMonthlyUnitSales] => 750
    [created_at] => 750
)
  • Related