【问题标题】:merge two arrays into one array将两个数组合并为一个数组
【发布时间】:2012-07-02 16:15:02
【问题描述】:

我有以下代码为我的 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);

?>

输出

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 的默认属性,另外两个数组是带有s7: 前缀的属性。但我需要的是 $result$result1 在 foreach 循环中合并,这样每个 $textNode 包含所有属性只有一个合并数组。因此,在这个示例 url 中,我最后应该有两个数组,因为只有两个 RichText 元素。

这可能吗?

【问题讨论】:

    标签: php xml


    【解决方案1】:

    这段代码:

    $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; 
        } 
    }
    

    【讨论】:

    • 我做了你推荐的更改,当我做print_r($attributesByNode); 时,我得到的只是:Array ( [0] =&gt; [1] =&gt; )。我的数据去哪儿了?
    • @thindery - 糟糕,SimpleXML 不返回数组。固定。
    猜你喜欢
    • 2021-07-16
    • 2012-11-19
    • 2012-05-10
    • 2018-04-27
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多