【发布时间】: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