2012-07-02 4 views
2

내 xml 소스의 각 RichText 요소에 대해 두 개의 배열을 만드는 다음 코드가 있습니다. 배열 중 하나는 접두어가있는 속성 용이고 다른 배열은 접두사가없는 속성 용입니다.두 배열을 하나의 배열로 병합

<?php 
$url = "http://testvipd7.scene7.com/is/agm/papermusepress/HOL_12_F_green?&fmt=fxgraw";  
$xml = simplexml_load_file($url);  
$xml->registerXPathNamespace('default', 'http://ns.adobe.com/fxg/2008'); 
$xml->registerXPathNamespace('s7', 'http://ns.adobe.com/S7FXG/2008'); 
$textNode = $xml->xpath("//default:RichText[@s7:elementID]"); 

function pr($var) { print '<pre>'; print_r($var); print '</pre>'; } 

$result = array(); 
$result1 = array(); 
foreach($textNode as $node){ 
    $result[] = $node->attributes('http://ns.adobe.com/S7FXG/2008'); 
    $result1[] = $node->attributes(); 

} 

$text = array_merge($result,$result1); 

pr($text); 

?> 

OUTPUT

Array 
(
    [0] => SimpleXMLElement Object 
     (
      [@attributes] => Array 
       (
        [caps] => none 
        [colorName] => 
        [colorValue] => #518269 
        [colorspace] => rgb 
        [elementID] => smalltext 
        [fill] => true 
        [fillOverprint] => false 
        [firstBaselineOffset] => ascent 
        [joints] => miter 
        [maxFontSize] => 11 
        [miterLimit] => 4 
        [referencePoint] => inherit 
        [rowCount] => 1 
        [rowGap] => 18 
        [rowMajorOrder] => true 
        [stroke] => false 
        [strokeOverprint] => false 
        [warpBend] => 0.5 
        [warpDirection] => horizontal 
        [warpHorizontalDistortion] => 0 
        [warpStyle] => none 
        [warpVerticalDistortion] => 0 
        [weight] => 1 
       ) 

     ) 

    [1] => SimpleXMLElement Object 
     (
      [@attributes] => Array 
       (
        [caps] => none 
        [colorName] => 
        [colorValue] => #518269 
        [colorspace] => rgb 
        [elementID] => largetext 
        [fill] => true 
        [fillOverprint] => false 
        [firstBaselineOffset] => ascent 
        [joints] => miter 
        [maxFontSize] => 19 
        [miterLimit] => 4 
        [referencePoint] => inherit 
        [rowCount] => 1 
        [rowGap] => 18 
        [rowMajorOrder] => true 
        [stroke] => false 
        [strokeOverprint] => false 
        [warpBend] => 0.5 
        [warpDirection] => horizontal 
        [warpHorizontalDistortion] => 0 
        [warpStyle] => none 
        [warpVerticalDistortion] => 0 
        [weight] => 1 
       ) 

     ) 

    [2] => SimpleXMLElement Object 
     (
      [@attributes] => Array 
       (
        [x] => 278.418 
        [y] => 115.542 
        [columnGap] => 18 
        [columnCount] => 1 
        [textAlign] => left 
        [fontFamily] => Trade Gothic LT Pro Bold Cn 
        [fontSize] => 11 
        [color] => #518269 
        [whiteSpaceCollapse] => preserve 
        [width] => 212.582 
        [height] => 33 
       ) 

     ) 

    [3] => SimpleXMLElement Object 
     (
      [@attributes] => Array 
       (
        [x] => 278.998 
        [y] => 86.7168 
        [columnGap] => 18 
        [columnCount] => 1 
        [textAlign] => left 
        [fontFamily] => Bootstrap 
        [fontSize] => 19 
        [color] => #518269 
        [whiteSpaceCollapse] => preserve 
        [trackingRight] => 4% 
        [width] => 240 
        [height] => 29 
       ) 

     ) 

) 

array_merge 루프가 4 개 중첩 된 배열을 하나 개의 거대한 배열을 생성 한 후이 내가 그것을 할 방법을 알아낼 수있는 유일한 방법입니다. 두 개의 배열은 기본 속성이 RichText이고 나머지 두 배열은 접두사가있는 속성입니다. 그러나 내가 필요한 것은 $result$result1이 foreach 루프 안에 합쳐져서 모든 속성을 포함하는 각 $textNode에 대해 하나의 병합 된 배열이 있다는 것입니다. 따라서이 예제에서는 두 개의 배열이 있어야합니다. 두 개의 배열이 RichText이므로 두 개가 있어야합니다.

이것이 가능합니까?

답변

2

코드의이 비트 : 나는 거의 알고 생각으로

$text[] = array_merge($result,$result1); 

가 논리적으로 foreach 루프 내부에 속한다. 그러나, 거기에서 작동하지 않습니다. SimpleXML은 반복 될 수 있지만 배열 자체가 아닌 SimpleXMLElement 객체를 반환하기 때문에 array_merge을 다른 것으로 대체해야합니다. 모두 그래서

모든 :

$attributesByNode = array(); 

foreach($textNode as $node) { 
    $result1 = $node->attributes('http://ns.adobe.com/S7FXG/2008'); 
    $result2 = $node->attributes(); 

    foreach ($result1 as $attribName => $attributeValue) 
    { 
     $attributesByNode[$attribName] = $attribVal; 
    } 
    foreach ($result2 as $attribName => $attributeValue) 
    { 
     $attributesByNode[$attribName] = $attribVal; 
    } 
} 
+0

난 당신의 권장 변경을 나는'인 print_r ($의 attributesByNode)을 할 때 '내가 가진 전부입니다'배열 ([0] => [1] =>)'. 내 데이터는 어디로 갔습니까? – thindery

+0

@thindery - SimpleXML은 배열을 반환하지 않습니다. 결정된. –

관련 문제