【问题标题】:Create element on a nested multidimensional array with the keys based on a another array使用基于另一个数组的键在嵌套多维数组上创建元素
【发布时间】:2022-01-06 14:41:11
【问题描述】:

我试图在不知道多维数组键的情况下将一个小数组添加到多维数组中

我有一个如下所示的数组:

array:1 [▼
  3 => array:3 [▼
    "attribute_code" => "category"
    "attribute_id" => 3
    "attribute_value" => array:1 [▼
      4 => array:3 [▼
        "attribute_code" => "hardware"
        "attribute_id" => 4
        "attribute_value" => array:1 [▼
          5 => array:3 [▼
            "attribute_code" => "harddisk"
            "attribute_id" => 5
            "attribute_value" => array:1 [▼
              6 => array:7 [▼
                "attribute_code" => "small"
                "attribute_id" => 6
                "attribute_value" => "132gb"
                "attribute_value_id" => 3
                "attribute_parent_id" => 5
                "attribute_value_is_array" => false
                "attribute_value_is_multidimensional_array" => false
              ]
            ]
          ]
        ]
      ]
    ]
  ]
]

我还有一个数组:

array:1 [▼
  7 => array:6 [▼
    "attribute_code" => "medium"
    "attribute_id" => 7
    "attribute_value" => "264gb"
    "attribute_value_id" => 4
    "attribute_value_is_array" => false
    "attribute_value_is_multidimensional_array" => false
  ]
]

基本上我想要做的是将较小的数组添加到“硬盘”数组中,因此结果应该如下所示:

array:1 [▼
  3 => array:3 [▼
    "attribute_code" => "category"
    "attribute_id" => 3
    "attribute_value" => array:1 [▼
      4 => array:3 [▼
        "attribute_code" => "hardware"
        "attribute_id" => 4
        "attribute_value" => array:1 [▼
          5 => array:3 [▼
            "attribute_code" => "harddisk"
            "attribute_id" => 5
            "attribute_value" => array:1 [▼
              6 => array:7 [▼
                "attribute_code" => "small"
                "attribute_id" => 6
                "attribute_value" => "132gb"
                "attribute_value_id" => 3
                "attribute_parent_id" => 5
                "attribute_value_is_array" => false
                "attribute_value_is_multidimensional_array" => false
              ],
              7 => array:6 [▼
                "attribute_code" => "medium"
                "attribute_id" => 7
                "attribute_value" => "264gb"
                "attribute_value_id" => 4
                "attribute_value_is_array" => false
                "attribute_value_is_multidimensional_array" => false
              ]
            ]
          ]
        ]
      ]
    ]
  ]
]

我的问题是我想基于另一个带有键的数组动态添加较小的数组

array:6 [▼
  0 => 3
  1 => "attribute_value"
  2 => 4
  3 => "attribute_value"
  4 => 5
  5 => "attribute_value"
]

所以我尝试动态构建这样的东西:

$array[3]['attribute_value'][4]['attribute_value'][5]['attribute_value'] = $smallArray

【问题讨论】:

  • 到目前为止你尝试过什么?我想你可能只是用键循环数组来为其赋值。你最好检查一下isset的key是否有错误,如果有任何错误,请提前中断。
  • 你能给我一个例子,如何用循环来做它并给它赋值吗?

标签: php arrays multidimensional-array


【解决方案1】:

修改现有数组的递归解决方案。

function appendToValues(&$array, $value)
{
    foreach ($array as &$item) {
        if ($item['attribute_code'] === 'harddisk') {
            $item['attribute_value'][] = $value;
        } elseif(is_array($item['attribute_value'])) {
            appendToValues($item['attribute_value'], $value);
        }
    }
}

appendToValues($array, ['attribute_code' => 'medium', 'attribute_value' => '256gb']);

Sample

【讨论】:

  • 谢谢你帮我解决了这个问题!
猜你喜欢
  • 2022-01-03
  • 2021-07-20
  • 2019-05-12
  • 2018-04-19
  • 1970-01-01
  • 2021-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多