2011-12-21 3 views
0

PHP에서 다차원 배열을 재정렬해야하지만 올바르게 이해할 수 없으며 앞으로 foreach, for 및 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

답변

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]'에있는 3 개의 할당이 서로 덮어 쓰지는 않습니까? – Andy

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

뭔가로 변경 한 후 좀 더 사용자의 요구에 맞게 처리 ..

을 그 대상 : 모든 차원 배열을 걸어 (그것의 키를 저장 in $ keyType) 모든 항목을 거쳐 원하는 방식으로 새 배열에 추가하십시오. 모든 유형에 동일한 유형의 항목이 있다고 가정합니다.