![](/img/trans.png)
[英]How to build the keys of a multidimensional array in PHP from the values of another array?
[英]PHP change array keys with key values from another array
我有2个数组。 我正在尝试用相应的$ data键替换$ choices数组键
这是我期望看到的。 我希望这是有道理的
$choices = array(
'fruits' => array(
'Red Apple' => '',
'Yellow Banana' => '',
'Orange Orange' => '',
),
...
...
);
这是我的代码
$data = array(
'fruits' => array(
'apple' => 'Red Apple',
'banana' => 'Yellow Banana',
'orange' => 'Orange Orange',
),
'vegetables' => array(
'potato' => 'Brown Potato',
'carrot' => 'Orange Carrot',
'cabbage' => 'Green Cabbeage',
),
'vehicles' => array(
'car' => 'Small Car',
'plane' => 'Large Plane',
'train' => 'Medium Train',
)
);
$choices = array(
'fruits' => array(
'apple' => 'gjhgfj',
'banana' => 'gjfgjfg',
'orange' => 'gfjfgjfg'
),
'vegetables' => array(
'potato' => 'gjfgj',
'carrot' => 'gjfgj',
'cabbage' => 'gjfgj'
),
'vehicles' => array(
'car' => 'gjfgj',
'plane' => 'gfjgfjfgj',
'train' => 'gjfgjghj'
)
);
$choice = 'fruits';
if(array_key_exists($choice, $choices)) {
$array = $choices[$choice];
//this is where i want to swap array keys
}
更新
根据@andrew的回答,这就是我现在所拥有的
if(array_key_exists($choice, $choices)) {
$array = $choices[$choice];
//this is where i want to swap array keys
for ($i = 0; $i < count($choices); $i++) {
$array[$i] = array_combine(array_keys($data[$i]), $array[$i]);
}
}
大概是这样的:
foreach ($data as $key => $val){
$choices[$key] = $val;
array_flip($choices[$key]);
}
已编辑,但不能100%确定您要实现的目标,但这可能会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.