1

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.

5
  • what do you mean by optimize or simplify? are you facing any errors or is this simply a school assignment? Commented Jun 28, 2022 at 4:10
  • @Mashtan I didn't face any issue. I want to optimize the foreach loop. Commented Jun 28, 2022 at 5:36
  • The core question is why do you need to optimize, and what characteristic do you need to optimize? Speed? memory? Often you can get more of one at the expense of the other. As far as simplify goes, you've done a pretty good job keeping things clear. It's likely that you could crunch things down into a single loop, but unless you have a specific reason to do so, I'd sacrifice potential CPU milliseconds for real Engineer Hours, and move on to the next task. Commented Jun 28, 2022 at 8:29
  • @Jerry I want to do this a single loop instead of using multiple foreach loops. Commented Jun 28, 2022 at 8:53
  • Why? Have you measured the performance of this code and found it insufficient? Is there a reason you think a single loop would be better? Commented Jun 28, 2022 at 10:54

1 Answer 1

1

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

1 Comment

@GenImpact, does this answer your question?

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.