Array
(
[0] => Array
(
[user_id] => 40718
[name] => abc1
)
[1] => Array
(
[user_id] => 40718
[name] => abc2
)
[2] => Array
(
[user_id] => 40719
[name] => abc3
)
)
我的数组就像拥有user_id一样,你可以在上面看到我希望根据数组中的特定值将其转换为嵌套数组,如user_id,如下所述
Array
(
[40718] => Array
(
[0]=>array(
[name] => abc1
)
[1]=>array(
[name] => abc2
)
)
[40719] => Array
(
[0] => (
[name] => abc3
)
)
)
答案 0 :(得分:-1)
虽然我还没有对其进行测试,但请检查它是否可以帮助您获得结果:
<?php
$arrTest = [
[
'user_id' => 40718,
'name' => 'abc1'
],
[
'user_id' => 40718,
'name' => 'abc2'
],
[
'user_id' => 40719,
'name' => 'abc3'
]
];
$resultArr = [];
foreach ($arrTest as $val) {
$resultArr[$val['user_id']][]['name'] = $val['name'];
}
echo '<pre>'; print_r($resultArr); exit;
?>