使用基于另一个数组的键在嵌套多维数组上创建元素
Create element on a nested multidimensional array with the keys based on a another array
我试图在不知道多维数组的键的情况下将一个小数组添加到多维数组
我有一个如下所示的数组:
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
修改现有数组的递归解决方案。
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']);
我试图在不知道多维数组的键的情况下将一个小数组添加到多维数组
我有一个如下所示的数组:
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
修改现有数组的递归解决方案。
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']);