我正在尝试在数组中重新排列给我的数据,以便更易于管理
这是给定的数组
$config = array(
"themes" => array(
"0" => array(
"Name" => 'connect',
"Description" => 'Connect your song to the previous song by reusing a word in the artist or title.'
),
"1" => array(
"Name" => 'color',
"Description" => 'Play songs with a color in the artist name or song title, the song could also have something to do with color.'
)
)
);
这是所需的输出
$desired_config = array(
"themes" => array(
"connect" => array(
"0" => 'Connect your song to the previous song by reusing a word in the artist or title.'
),
"color" => array(
"0" => 'Play songs with a color in the artist name or song title, the song could also have something to do with color.'
)
)
);
这就是我尝试的
foreach($config as $key=>$value){
if(is_array($value)){
foreach($value as $index=>$object){
if(is_array($object)){
foreach($object as $command){
$config[$key][$command['Name']][] = $command['Description'];
}
}else{
$config[$key] = $value;
}
}
}
}
print_r($config);
我的成绩很差
Array
(
[themes] => Array
(
[0] => Array
(
[Name] => connect
[Description] => Connect your song to the previous song by reusing a word in the artist or title.
)
[1] => Array
(
[Name] => color
[Description] => Play songs with a color in the artist name or song title, the song could also have something to do with color.
)
[c] => Array
(
[0] => c
[1] => c
)
[C] => Array
(
[0] => C
)
[P] => Array
(
[0] => P
)
)