Chinmay235
Chinmay235

Reputation: 3414

PHP assign one array value to another array

array (
  0 => 
  array (
    'label' => '1',
    'index' => 1,
    'product_attributes' => 
    array (
      0 => 
      array (
        'type' => 'product',
        'id' => 1,
        'label' => 'Size',
        'placeholder' => 'Select Size',
        'description' => '',
        'defaultValue' => 
        array (
          'text' => 'Size32',
          'price' => '22',
          'isSelected' => false,
        ),
        'choices' => 
        array (
          0 => 
          array (
            'text' => 'Size30',
            'price' => '20',
            'isSelected' => 'true',
          ),
          1 => 
          array (
            'text' => 'Size32',
            'price' => '22',
            'isSelected' => false,
          ),
        ),
        'conditionalLogic' => '',
      ),
      1 => 
      array (
        'type' => 'product',
        'id' => 2,
        'label' => 'Color',
        'placeholder' => 'Select Color',
        'description' => 'DEsc',
        'defaultValue' => 
        array (
          'text' => 'Black',
          'price' => '5',
          'isSelected' => 'false',
        ),
        'choices' => 
        array (
          0 => 
          array (
            'text' => 'Black',
            'price' => '5',
            'isSelected' => 'false',
          ),
          1 => 
          array (
            'text' => 'Green',
            'price' => '6',
            'isSelected' => 'false',
          ),
          2 => 
          array (
            'text' => 'Blue',
            'price' => '4',
            'isSelected' => 'true',
          ),
          3 => 
          array (
            'text' => 'White',
            'price' => '1',
            'isSelected' => 'false',
          ),
        ),
        'conditionalLogic' => '',
      ),
      2 => 
      array (
        'type' => 'product',
        'id' => 3,
        'label' => 'Fit',
        'placeholder' => 'Select Fit',
        'description' => 'Select Fit',
        'defaultValue' => 
        array (
        ),
        'choices' => 
        array (
          0 => 
          array (
            'text' => 'Slim',
            'price' => '2',
            'isSelected' => false,
          ),
          1 => 
          array (
            'text' => 'Regular',
            'price' => '3',
            'isSelected' => false,
          ),
          2 => 
          array (
            'text' => 'Casual',
            'price' => '5',
            'isSelected' => false,
          ),
        ),
        'conditionalLogic' => '',
      ),
    ),
    'total_product_cost' => '$27.00',
    'total_product_price' => '27.00',
    'product_id' => '36',
  ),
  1 => 
  array (
    'label' => 'label21',
    'total_product_cost' => '$27.00',
    'total_product_price' => '27.00',
    'index' => 3,
    'product_id' => '36',
    'product_attributes' => 
    array (
      0 => 
      array (
        'type' => 'product',
        'id' => 1,
        'label' => 'Size',
        'placeholder' => 'Select Size',
        'description' => '',
        'defaultValue' => 
        array (
          'text' => 'Size32',
          'price' => '22',
          'isSelected' => false,
        ),
        'choices' => 
        array (
          0 => 
          array (
            'text' => 'Size30',
            'price' => '20',
            'isSelected' => 'true',
          ),
          1 => 
          array (
            'text' => 'Size32',
            'price' => '22',
            'isSelected' => false,
          ),
        ),
        'conditionalLogic' => '',
        'conditionalLogic2' => 
        array (
          'actionType' => 'show',
          'logicType' => 'all',
          'checkbox' => true,
          'rules' => 
          array (
            0 => 
            array (
              'fieldId' => 2,
              'operator' => 'is',
              'value' => 'Black',
            ),
          ),
        ),
      ),
      1 => 
      array (
        'type' => 'product',
        'id' => 2,
        'label' => 'Color',
        'placeholder' => 'Select Color',
        'description' => 'DEsc',
        'defaultValue' => 
        array (
          'text' => 'Black',
          'price' => '5',
          'isSelected' => 'false',
        ),
        'choices' => 
        array (
          0 => 
          array (
            'text' => 'Black',
            'price' => '5',
            'isSelected' => 'false',
          ),
          1 => 
          array (
            'text' => 'Green',
            'price' => '6',
            'isSelected' => 'false',
          ),
          2 => 
          array (
            'text' => 'Blue',
            'price' => '4',
            'isSelected' => 'true',
          ),
          3 => 
          array (
            'text' => 'White',
            'price' => '1',
            'isSelected' => 'false',
          ),
        ),
        'conditionalLogic' => '',
      ),
      2 => 
      array (
        'type' => 'product',
        'id' => 3,
        'label' => 'Fit',
        'placeholder' => 'Select Fit',
        'description' => 'Select Fit',
        'defaultValue' => 
        array (
        ),
        'choices' => 
        array (
          0 => 
          array (
            'text' => 'Slim',
            'price' => '2',
            'isSelected' => false,
          ),
          1 => 
          array (
            'text' => 'Regular',
            'price' => '3',
            'isSelected' => false,
          ),
          2 => 
          array (
            'text' => 'Casual',
            'price' => '5',
            'isSelected' => false,
          ),
        ),
        'conditionalLogic' => '',
      ),
    ),
  ),
)

I have posted my array value. This values are dynamic. Here is two array conditionalLogic and conditionalLogic2 I want to assign those array key conditionalLogic2 exist and value should be assign to conditionalLogic. After assign conditionalLogic values into the conditionalLogic2 remove that key from my array list.

Check I have tried this way but not working -

// $data['values'] array I have posted above
foreach ($data['values'] as $products) {
    foreach ($products['product_attributes'] as $product_choices) {
        if (!empty($product_choices['conditionalLogic2']) && $product_choices['conditionalLogic'] == '') {
            $product_choices['conditionalLogic'] = $product_choices['conditionalLogic2'];
            unset($product_choices['conditionalLogic2']);
        }
    }
}
var_export($data['values']); exit;

Please help me and give me any solution how to replace the array value into another array.

What is the shortcut way to solve this problem?

Upvotes: 1

Views: 734

Answers (3)

sevavietl
sevavietl

Reputation: 3802

It is usually better to avoid nested loops:

foreach ($data as &$datum) {
    if (!isset($datum['product_attributes'])) {
        continue;
    }

    $datum['product_attributes'] = array_map(function ($productAttribute) {
        if (
            !empty($productAttribute['conditionalLogic2']) 
            && empty($productAttribute['conditionalLogic'])
        ) {
            $productAttribute['conditionalLogic'] = $productAttribute['conditionalLogic2'];
            unset($productAttribute['conditionalLogic2']);
        }

        return $productAttribute;
    }, $datum['product_attributes']);
}

Here I used array_map() function to assign new arrays directly.

Here is working example.

Upvotes: 2

Max Zuber
Max Zuber

Reputation: 1229

Your code is almost right, but you're acting a local variables in foreach loops. You should bind them to original array items as follows:

foreach ($data['values'] as & $products) {
    foreach ($products['product_attributes'] as & $product_choices) {
        if (!empty($product_choices['conditionalLogic2']) && $product_choices['conditionalLogic'] == '') {
            $product_choices['conditionalLogic'] = $product_choices['conditionalLogic2'];
            unset($product_choices['conditionalLogic2']);
        }
    }
}
unset($products);
unset($product_choices);

If there is the end of current function scope, both unset may be omitted. But you may remove references from an array explicitly to avoid undesired affects in some code below in the same scope. I.e. $products = 10; somewhere below crushes your last branch of an array.

Upvotes: 2

Parminder Singh
Parminder Singh

Reputation: 351

@chinu You are actually assigning or changing the local variables where scope is limited within for loop only so in this case, we should use pass by reference.

See the following code, it will work:

foreach ($data['values'] as &$products) {
    foreach ($products['product_attributes'] as &$product_choices) {        
        if (!empty($product_choices['conditionalLogic2']) && $product_choices['conditionalLogic'] == '') {
            $product_choices['conditionalLogic'] = $product_choices['conditionalLogic

Upvotes: 1

Related Questions