我有一个像这样的关联数组
(
[0] => Array
(
[userId] => 4785
[courseId] => 1774
[EnterpriseId] => 13
[lbaseid] => 1697
[progress] => 100
[milestone] => 5
)
[1] => Array
(
[userId] => 4786
[courseId] => 1775
[EnterpriseId] => 13
[lbaseid] => 1698
[progress] => 100
[milestone] => 5
)
[2] => Array
(
[userId] => 4786
[courseId] => 1776
[EnterpriseId] => 13
[lbaseid] => 1699
[progress] => 100
[milestone] => 5
)
[3] => Array
(
[userId] => 4786
[courseId] => 1777
[EnterpriseId] => 13
[lbaseid] => 1700
[progress] => 100
[milestone] => 5
)
[4] => Array
(
[userId] => 4786
[courseId] => 1778
[EnterpriseId] => 13
[lbaseid] => 1701
[progress] => 100
[milestone] => 5
)
)
我想像这样转换关联数组
(
4785_1774_13[0] => Array
(
[userId] => 4785
[courseId] => 1774
[EnterpriseId] => 13
[lbaseid] => 1697
[progress] => 100
[milestone] => 5
)
4786_1775_13[1] => Array
(
[userId] => 4786
[courseId] => 1775
[EnterpriseId] => 13
[lbaseid] => 1698
[progress] => 100
[milestone] => 5
)
4786_1776_13[2] => Array
(
[userId] => 4786
[courseId] => 1776
[EnterpriseId] => 13
[lbaseid] => 1699
[progress] => 100
[milestone] => 5
)
4786_1777_13[3] => Array
(
[userId] => 4786
[courseId] => 1777
[EnterpriseId] => 13
[lbaseid] => 1700
[progress] => 100
[milestone] => 5
)
4786_1778_13[4] => Array
(
[userId] => 4786
[courseId] => 1778
[EnterpriseId] => 13
[lbaseid] => 1701
[progress] => 100
[milestone] => 5
)
)
答案 0 :(得分:0)
如果您要使用由原始数组中的值构造的键组成一个新数组,则可以遍历初始数组并以所需的键格式构造一个新数组。
$array = [
[
"userId" => 11,
"courseId" => 22,
"EnterpriseId" => 33
],
[
"userId" => 44,
"courseId" => 55,
"EnterpriseId" => 66
]
];
var_dump($array);
$new_array = [];
foreach($array as $key => $value) {
$new_key = implode("_",[
$value["userId"],
$value["courseId"],
$value["EnterpriseId"]
]);
$new_array[$new_key] = $value;
}
var_dump($new_array);
此处为实时示例:https://3v4l.org/1ibMl