2017-02-17 50 views
-1

我想如果key存在且数组不多维例如插入前阵的额外的键:PHP多维arrray迭代recursevely

Array 
(
    [0] => Array 
     (
      [_key_] => Array 
       (
        [0] => Array 
         (
          [pub-id-type] => pmid 
          [value] => 25588809 
         ) 

        [1] => Array 
         (
          [pub-id-type] => pmc 
          [value] => 4302133 
         ) 

        [2] => Array 
         (
          [pub-id-type] => publisher-id 
          [value] => 1008 
         ) 

        [3] => Array 
         (
          [pub-id-type] => doi 
          [value] => 10.1186/s12885-015-1008-4 
         ) 

        [type_s] => article-id 
        [id] => 58a6eeedeab2f 
       ) 

     ) 

我想:

Array 
(
    [0] => Array 
     (
      [_key_] => Array 
       (
[0]=>array(
        [0] => Array 
         (
          [pub-id-type] => pmid 
          [value] => 25588809 
         ) 

        [1] => Array 
         (
          [pub-id-type] => pmc 
          [value] => 4302133 
         ) 

        [2] => Array 
         (
          [pub-id-type] => publisher-id 
          [value] => 1008 
         ) 

        [3] => Array 
         (
          [pub-id-type] => doi 
          [value] => 10.1186/s12885-015-1008-4 
         ) 

        [type_s] => article-id 
        [id] => 58a6eeedeab2f 
       ) 
     ) 
) 

递归UTILñdepath找到_Key_

+0

什么是你为这个上面的一个期望的数组? –

回答

0

布局是不是最好的,但howerver ...

试试这个:

if(is_array($arr[0][_key_])) 
{ 
    $tmp = $arr[0][_key_]; 
    unset($arr[0][_key_]); 
    $arr[0][_key_][] = $tmp; 
}