0

What is the best way to convert the following array in a way where the output will be as shown below.

Initial Array

array:2 [▼
  0 => array:4 [▼
    "group" => "1"
    4 => "19"
    6 => "27"
    8 => "160"
  ]
  1 => array:4 [▼
    "group" => "2"
    4 => "20"
    6 => "28"
    8 => "200"
  ]
]

the desired array:

array:6 [▼
  0 => array:3 [▼
       "group" => "1"
       "es_variation_set_id" => "4" << this is key form the initial array
       "es_variation_id" => "19" << this is value form the initial array
 ]
  1 => array:3 [▼
       "group" => "1"
       "es_variation_set_id" => "6" << this is key form the initial array
       "es_variation_id" => "28" << this is value form the initial array
 ]
  2 => array:3 [▼
       "group" => "1"
       "es_variation_set_id" => "8" << this is key form the initial array
       "es_variation_id" => "160" << this is value form the initial array
 ]
  3 => array:3 [▼
       "group" => "2"
       "es_variation_set_id" => "4" << this is key form the initial array
       "es_variation_id" => "20" << this is value form the initial array
 ]
  4 => array:3 [▼
       "group" => "2"
       "es_variation_set_id" => "6" << this is key form the initial array
       "es_variation_id" => "28" << this is value form the initial array
 ]
  5 => array:3 [▼
       "group" => "1"
       "es_variation_set_id" => "8" << this is key form the initial array
       "es_variation_id" => "200" << this is value form the initial array
 ]       
]

Here is my foreach

    foreach ($request->only('product_variations')['product_variations'] as $variation_value)
    {

        if($variation_value['group'] != 0){
            dd($variation_value);
        }
    }

please suggest the best way to go about this issue

Thanks in advance

2 Answers 2

1

You can tackle this with a nested foreach loop that pulls out the group value before running each time.

$output = [];
foreach ($input as $subArray) {
    $group = $subArray['group'];
    unset($subArray['group']);
    foreach ($subArray as $setId => $variationId) {
        $output[] = [
            'group' => $group,
            'es_variation_set_id' => $setId,
            'es_variation_id' => $variationId,
        ];
    }
}

Demo here: https://3v4l.org/KLf8Y

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

1 Comment

Thank you Chris, your solution is perfect, this is the one I am going to go with as it seems to go better with my other goals here
1

You are almost there. You just need to add another foreach in the if condition to individually add the group, the key and the value pairs.

<?php

$res = [];

foreach ($request->only('product_variations')['product_variations'] as $variation_value){
    if($variation_value['group'] != 0){
        foreach($variation_value as $k => $v){
            if($k == 'group') continue;
            $res[] = [
                'group' => $variation_value['group'],
                'es_variation_set_id' => $k,
                'es_variation_id' => $v
            ];
        }
    }
}

print_r($res);

1 Comment

thank you very much for such a fast response, your solution worked perfectly

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.