【问题标题】:PHP assign one array value to another arrayPHP 将一个数组值分配给另一个数组
【发布时间】:2016-10-05 23:20:27
【问题描述】:
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;

请帮助我并给我任何解决方案如何将数组值替换为另一个数组。

解决这个问题的捷径是什么?

【问题讨论】:

  • 什么是$data['values'] 我在你的数据结构中没有看到任何东西
  • 首先,当您遍历某个数组时,您使用的是元素的副本,而不是元素本身。要使用该元素,您需要引用它foreach ($data['values'] as &$products) {。
  • $data['values'] 数据超出数组格式
  • 那就试试foreach ($products['product_attributes'] as &$product_choices) {
  • 并尝试使用var_export() 而不是var_dump()。这样,您的数组表示可以被复制/粘贴。

标签: php arrays


【解决方案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()函数直接分配新数组。

Here is working example.

【讨论】:

  • :) :) :) :) OP 无法理解如何处理一个相当简单的数组。你认为将匿名函数作为参数传递给它们的其他函数是个好主意。好吧,我认为这只是为了证明您有多聪明,而不是对 OP 有任何帮助
  • 我看到了你的回答。你的例子工作正常。但是我的本地机器无法正常工作。我收到此错误消息 - Notice (8): Undefined index: product_attributes [APP\Controller\OrdersController.php, line 378] Warning (2): array_map() [function.array-map]: Argument #2 should be an array [APP\Controller\OrdersController.php, line 378] Notice (8): Undefined index: product_attributes [APP\Controller\OrdersController.php, line 378] Warning (2): array_map() [function.array-map]: Argument #2 should be an array [APP\Controller\OrdersController.php, line 378]
  • @Chinu 好像你的某些数组没有'product_attributes',我会更新代码。
  • @Chinu 更新。
【解决方案2】:

您的代码几乎是正确的,但您在 foreach 循环中充当局部变量。您应该将它们绑定到原始数​​组项,如下所示:

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 都可以省略。但是您可以显式地从数组中删除引用,以避免在同一范围内的某些代码中产生不良影响。 IE。 $products = 10; 下面某处会破坏数组的最后一个分支。

【讨论】:

  • 不知道为什么最后有 2 unset()
  • @RiggsFolly 我在回答中评论了这方面
  • 是的,但是仅仅添加 4 行代码并没有什么大不了的,所以对于 OP 来说,正在发生的事情是显而易见的。
【解决方案3】:

@chinu 您实际上是在分配或更改范围仅限于 for 循环内的局部变量,因此在这种情况下,我们应该使用按引用传递。

看下面的代码,就可以了:

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

【讨论】:

  • 为什么 OP 应该“试试这个”? 好的答案将始终解释所做的工作以及这样做的原因,不仅适用于 OP,而且适用于可能会发现此问题并正在阅读您的答案的 SO 的未来访问者。
  • 他的其余代码发生了什么。大多数初学者都喜欢我们尽可能简单地理解答案。如果你留下太多他们不理解的想象力
  • 帖子中没有其他代码,如果我遗漏了什么,请告诉我。我修复了有问题的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-29
  • 2018-08-22
  • 2014-07-14
  • 2016-06-04
  • 2012-12-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多