2011-12-21 89 views
0

我需要在php中重新排列一個多維數組,但是我無法做到正確,我現在正在爲foreach和while循環苦苦掙扎。重新排列多維php陣列的故障

這是我有:

Array 
(
[dimension] => Array 
    (
     [dimension.part] => Array 
      (
       [0] => SimpleXMLElement Object 
        (
         [0] => geheel 
        ) 

       [1] => SimpleXMLElement Object 
        (
         [0] => geheel 
        ) 

      ) 

     [dimension.type] => Array 
      (
       [0] => SimpleXMLElement Object 
        (
         [0] => hoogte 
        ) 

       [1] => SimpleXMLElement Object 
        (
         [0] => breedte 
        ) 

      ) 

     [dimension.value] => Array 
      (
       [0] => SimpleXMLElement Object 
        (
         [0] => 73 
        ) 

       [1] => SimpleXMLElement Object 
        (
         [0] => 84 
        ) 

      ) 

     [dimension.unit] => Array 
      (
       [0] => SimpleXMLElement Object 
        (
         [0] => cm 
        ) 

       [1] => SimpleXMLElement Object 
        (
         [0] => cm 
        ) 

      ) 

    ) 

[material] => Array 
    (
     [material.part] => Array 
      (
       [0] => SimpleXMLElement Object 
        (
         [0] => deelmateriaal 
        ) 

       [1] => SimpleXMLElement Object 
        (
         [0] => deelmateriaal2 
        ) 

      ) 

     [material_type] => Array 
      (
       [0] => SimpleXMLElement Object 
        (
         [0] => typemateriaal 
        ) 

       [1] => SimpleXMLElement Object 
        (
         [0] => typemateriaal2 
        ) 

      ) 

     [material] => Array 
      (
       [0] => SimpleXMLElement Object 
        (
         [0] => materiaal 
        ) 

       [1] => SimpleXMLElement Object 
        (
         [0] => materiaal2 
        ) 

      ) 

     [material.notes] => Array 
      (
       [0] => SimpleXMLElement Object 
        (
         [0] => notemateriaal 
        ) 

       [1] => SimpleXMLElement Object 
        (
         [0] => notemateriaal2 
        ) 

      ) 

    ) 

) 

,我需要把它轉換爲:

dimensions => Array (1 => array(dimension.part => geheel) 
         => array (dimension.type => hoogte)) 
       ..... 
      => Array (2 => array(dimension.part => ...) 
         => array (dimension.type => ...)) 
       .... 
      .... 
material => Array (1 => ... 
        2 => ... 
      => Array (1 => 
      => 
      .... 

任何人都得到一個好方法呢?

感謝,

里斯

+1

也許顯示你的一些鬥爭? – k102 2011-12-21 10:33:39

回答

0
$new_array = new array(); 
    $count = count($array['dimension']['dimension_part']); 
    for($i=0;$i<$count;$i++) { 
     $new_array[$i]['dimension.part'] = $array['dimension']['dimension.part'][$i][0]; 
     $new_array[$i]['dimension.type'] = $array['dimension']['dimension.type'][$i][0]; 
     $new_array[$i]['dimension.value'] = $array['dimension']['dimension.value'][$i][0]; 
    } 

我不太知道如何訪問simplexmlobjects,所以我用了數組類型。也許它更改爲[$i] -> 0

+0

這三個關於'$ new_array [$ i]'的作業不會互相覆蓋嗎? – Andy 2011-12-21 10:53:19

1
foreach($arr['dimension'] as $keyType => $type) { 
    foreach($type as $key => $value) { 
     $aNewArray[$key][$keyType] = $value; 
    } 
} 

類似的東西..然後處理它多一點,以滿足您的需求..

它所做的:走過每一個維數組(和存儲其關鍵在$ keyType中),然後遍歷每一個項目,並按照你想要的方式將它添加到一個新的數組中。它假定每種類型都有相同數量的項目。