Gen Impact
Gen Impact

Reputation: 23

Simplify & Optimize PHP Array

I am working on combining the array into a key with the count of repeated "option""code". My Request JSON is like this

[{
    "productId": "DENSUS-MARK",
    "options": [
        {
            "code": "HIGLIGT_OPTION_HANDLE"
        },
        {
            "code": "HIGLIGT_OPTION_HANDLE1"
        }
        
        
    ]
},
{
    "productId": "DENSUS-MARK",
    "options": [
        {
            "code": "HIGLIGT_OPTION_HANDLE"
        }
    ]
},
{
    "productId": "DENSUS-MARK-II",
    "options": [
        {
            "code": "HIGLIGT_OPTION_HANDLE"
        }
    ]
}]

After combing the "productID" and the count of ["options"]["code"] (For ProductId - DENSUS-MARK, the code "HIGLIGT_OPTION_HANDLE" count is 2. So I am getting a output like this.

 {
    "productId": "DENSUS-MARK",
    "options": [
        {
            "code": "HIGLIGT_OPTION_HANDLE",
            "count": 2
        },
                    {
            "code": "HIGLIGT_OPTION_HANDLE1",
            "count": 1
        }
    ]
},
    {
    "productId": "DENSUS-MARK-II",
    "options": [
        {
            "code": "HIGLIGT_OPTION_HANDLE",
            "count": 1
        }
    ]
}

}

This is my current php code and I need to optimize & simply this below code

    $datas = json_decode($arr,true);
    $formattedData = [];
    foreach ($datas as $f) {
        foreach ($f['options'] as $option) {
            $formattedData[$f['productID']]['productID'] = $f['productID'];
            $formattedData[$f['productID']]['options']['code'][$option['code']][] = $option['code'];
        }
    }

    foreach ($formattedData as &$data) {
    $formattedOptions = [];
    foreach ($data['options']['code'] as $key => $codes) {
        $formattedOptions[] = [
            'code' => $key,
            'count' => count($codes)
        ];
    }
    $data = $formattedOptions;
    }

    print_r($formattedData);

Someone, could you please help me in this.

Upvotes: 1

Views: 69

Answers (1)

Monnomcjo
Monnomcjo

Reputation: 725

I don't know if this is the optimization you want. Meanwhile, less than two loops, I have not found. It's not quite the expected result, but you should be able to fix it if you need to.

With:

$input = array (
  0 => 
  array (
    'productId' => 'DENSUS-MARK',
    'options' => 
    array (
      0 => 
      array (
        'code' => 'HIGLIGT_OPTION_HANDLE',
      ),
      1 => 
      array (
        'code' => 'HIGLIGT_OPTION_HANDLE1',
      ),
    ),
  ),
  1 => 
  array (
    'productId' => 'DENSUS-MARK',
    'options' => 
    array (
      0 => 
      array (
        'code' => 'HIGLIGT_OPTION_HANDLE',
      ),
    ),
  ),
  2 => 
  array (
    'productId' => 'DENSUS-MARK-II',
    'options' => 
    array (
      0 => 
      array (
        'code' => 'HIGLIGT_OPTION_HANDLE',
      ),
    ),
  )
);

Then just:

$result = [];
foreach($input as $row) {
    foreach($row['options'] as $value) {
        $result[$row['productId']][$value['code']] ??=0;
        $result[$row['productId']][$value['code']] += count($value);
  }
}
var_export($result);

Results:

array (
  'DENSUS-MARK' => 
  array (
    'HIGLIGT_OPTION_HANDLE' => 2,
    'HIGLIGT_OPTION_HANDLE1' => 1,
  ),
  'DENSUS-MARK-II' => 
  array (
    'HIGLIGT_OPTION_HANDLE' => 1,
  ),
)

Upvotes: 1

Related Questions