Reputation: 1194
i have the following code that creates two arrays for each RichText
element in my xml source. One of the arrays is for an attribute with a prefix, and the other array is for the attributes that do not have any prefixes. This is the only way I could figure out how to do it:
<?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
)
)
)
My array_merge
after the loop creates one huge array, with 4 nested arrays. Two arrays are the default attributes of RichText
, and the other two arrays are the attributes that have a s7:
prefix. But what I need is that $result
and $result1
be merged inside the foreach loop, that way there is just one merged array for each $textNode
containing all the attributes. Thus, in this example url I should have two arrays in the end because there are only two RichText
elements.
Is this possible?
Upvotes: 2
Views: 437
Reputation: 13539
This bit of your code:
$text[] = array_merge($result,$result1);
logically belongs to inside of your foreach loop as I guess you almost knew. However, it does not work there. SimpleXML likes to return SimpleXMLElement objects that can be iterated, but are not arrays themselves, so we need to replace array_merge
by something else.
So all in all:
$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;
}
}
Upvotes: 2