我有以下数组作为db的响应。我正试图根据我的要求将此数据库响应转换为多维数组。
Array
(
[0] => Array
(
[0] => Array
(
[_id] => C10359
[AE] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
)
[1] => Array
(
[_id] => C10428
[AE] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
)
[1] => Array
(
[0] => Array
(
[_id] => C10350
[AE] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
)
[1] => Array
(
[_id] => C10430
[AE] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
)
)
现在我需要按照以下方式转换上面的数组。
Array
(
[0] => Array
(
[C10359] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
[C10428] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
[1] => Array
(
[C10350] => Array
(
[0] => 89785
[1] => 89786
[2] => 89857
[3] => 89859
)
[C10430] => Array
(
[0] => 50191
[1] => 50203
[2] => 50230
[3] => 50244
)
)
)
以下是我尝试的方法
array_map(function($arr) {
return $arr[0] ;
},$panel_result);
但它不工作。
请建议我如何转换所需的格式。
这应该能奏效:
$arr = array(
array(
array(
'_id' => 'C10359',
'AE' => array
(
89785,
89786,
89857,
89859,
),
),
array(
'_id' => 'C10428',
'AE' => array
(
50191,
50203,
50230,
50244,
),
),
),
);
$output = array();
foreach ($arr as $levelK => $level) {
if(!isset($output[$levelK])){
$output[$levelK] = array();
}
foreach ($level as $subLevel) {
$id = $subLevel['_id'];
if (!isset($output[$levelK][$id])) {
$output[$levelK][$id] = array();
}
foreach ($subLevel['AE'] as $val) {
$output[$levelK][$id][] = $val;
}
}
}
使用array_column()
并传递第三个参数作为索引键
$reqArray = array();
foreach ($yourArray as $key => $innerArray) {
$reqArray[] = array_column($innerArray, 'AE', '_id');
}
或
使用array map()
$reqArray = array_map(function($a){
return array_column($a, 'AE', '_id');
},$arr);