Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 向上移动数组项_Php_Arrays - Fatal编程技术网

Php 向上移动数组项

Php 向上移动数组项,php,arrays,Php,Arrays,我试图重新排列数组中提供给我的数据,以便更易于管理 这是给定的数组 $config = array( "themes" => array( "0" => array( "Name" => 'connect', "Description" => 'Connect your song to the previous song by reusing a word in the art

我试图重新排列数组中提供给我的数据,以便更易于管理

这是给定的数组

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

    )

我认为此功能可以满足您的需要:

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

我认为此功能将满足您的需要:

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

这将返回数组中的描述,但使我更接近原点,而不是描述。它应该是一个数组项,因为在任何“主题”中都可以有多个项。这将返回数组中的描述,但使我更接近原点,而不是描述。它应该是一个数组项,因为在任何主题中都可以有多个项“主题”maybe@mcgrailm我想你已经从Nadht那里得到了答案。这将在每次项目['name']相同时使用新数组覆盖,而不是将其添加到数组中。但是我想我可以从this@mcgrailm我想您已经从Nadht那里得到了答案。这将在每次项['name'出现时用一个新数组覆盖是相同的,而不是将其添加到阵列中,但我认为我可以从中得到我需要的
$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;
    }
}