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' => '',
),
),
),
)
我发布了我的数组值。这个值是动态的。这是两个数组conditionalLogic
和conditionalLogic2
我想分配那些数组键conditionalLogic2
存在,值应该分配给conditionalLogic
。将conditionalLogic
值分配到conditionalLogic2
后,从我的数组列表中删除该键。
检查我试过这种方式但没有工作 -
// $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;
请帮助我,并给我任何解决方案如何将数组值替换为另一个数组。
解决此问题的捷径是什么?
答案 0 :(得分:1)
您的代码几乎是正确的,但您在foreach循环中扮演局部变量。您应该将它们绑定到原始数组项,如下所示:
img{
display: inline-block;
width: 40%;
margin-right: 40px;
}
.right_column{
display: inline-block;
}
如果当前函数范围结束,则可以省略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);
。但是,您可以显式删除数组中的引用,以避免在同一范围内的某些代码中产生不良影响。即下面某处的unset
会破坏你阵列的最后一个分支。
答案 1 :(得分:1)
通常最好避免嵌套循环:
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']);
}
这里我使用array_map()
函数直接分配新数组。
答案 2 :(得分:0)
@chinu实际上,您实际上只是在for循环中分配或更改范围有限的局部变量,所以在这种情况下,我们应该使用pass by reference。
请参阅以下代码,它将起作用:
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