2013-07-21 81 views
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 
      ) 

    ) 

codepad demo

回答

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; 
    } 
} 
+0

有很多好帖子,但这个帮助我最多 – mcgrailm

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

这个返回数组的int描述,但得到我离家更近,而不是描述它应该是一个数组项,因为在任何“t”中都可以有多个项目血红素“也许 – mcgrailm

2
​​
+0

表决。准确的答案也很短的代码。 –

+0

@mcgrailm我认为你已经从Nadh –

+0

得到了答案,这将覆盖一个新的数组,每次一个项目['名称']是相同的,而不是将其添加到数组 – mcgrailm