0

我正在尝试在数组中重新排列给我的数据,以便更易于管理

这是给定的数组

$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
            )

    )

键盘演示

4

3 回答 3

2
$desired_config = array();
foreach($config["themes"] as $index => $item) {
    $desired_config["themes"][$item["Name"]] = array($item["Description"]);
}

print_r($desired_config);
于 2013-07-21T17:13:18.183 回答
1

我认为这个功能会做你需要的:

function shiftMyArray($myArray) {
$ret = array();
foreach ($myArray as $key => $value){
    if (is_array($value)){
        $newArray = array();
        foreach ($value as $index => $object){
            if (is_array($object)){
                $newArray[$object['Name']] = $object;
                unset($newArray[$object['Name']]['Name']);
            } else {
                $newArray[$index] = $object;
            }
        }
        $ret[$key] = $newArray;
    } else {
        $ret[$key] = $value;
    }
}
return $ret;
}
var_dump($config, shiftMyArray($config));
于 2013-07-21T17:11:51.417 回答
1
$new_config = array();
foreach ($config as $key=>$item) {
    if (is_array($item)) {
        $new_config[$key] = array();
        foreach($item as $value) {
            if (is_array($value) && array_key_exists('Name', $value) && array_key_exists('Description', $value)) {
                $new_config[$key][$value['Name']] = array($value['Description']);   
            } else {
                $new_config[$key][] = $value;
            }
        }
    } else {
        $new_config[$key] = $item;
    }
}
于 2013-07-21T17:19:32.713 回答