I recently asked a question about how to select a parent's node attribute and its values at the same time using xpath, What i ended up with is :
Parent/#attr|Parent/x
It grab the parent attr's value and all of its x's nodes..
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => attrValue
)
)
[1] => SimpleXMLElement Object
(
[0] => 1
)
[2] => SimpleXMLElement Object
(
[0] => 2
)
[3] => SimpleXMLElement Object
(
[0] => 3
)
)
The thing now.. In case there are several Parent nodes, it will mix them together and i wouldn't know which attr belongs to its x's .. it will result something like this :
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => attrValue
)
)
[1] => SimpleXMLElement Object
(
[0] => 1
)
[2] => SimpleXMLElement Object
(
[0] => 2
)
[3] => SimpleXMLElement Object
(
[0] => 3
)
[4] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => anotherAttrValue
)
)
[5] => SimpleXMLElement Object
(
[0] => 1
)
[6] => SimpleXMLElement Object
(
[0] => 2
)
...
)
You see, they are all treated as a normal result, what i am trying to do is to make the attr contain all of it's results ( 1, 2, 3 ) in an array or something and the same thing goes to the other attr's to end up with something like this :
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => attrValue
)
)
Array
(
[0] => SimpleXMLElement Object
(
[0] => 1
)
[1] => SimpleXMLElement Object
(
[0] => 2
)
[2] => SimpleXMLElement Object
(
[0] => 3
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[attr] => anotherAttrValue
)
)
Array
(
[0] => SimpleXMLElement Object
(
[0] => 1
)
[1] => SimpleXMLElement Object
(
[0] => 2
)
[2] => SimpleXMLElement Object
(
[0] => 3
)
)
)
using the xpath and the way given at first
Related
again I think I'm missing something really basic here, but I really can't figure it out.
I'm parsing an xml file formatted as excel, using SimpleXML in PHP.
My xml file looks like the following:
SimpleXMLElement Object (
[DocumentProperties] => SimpleXMLElement Object (
[Author] => Surname Name
[LastAuthor] => Name Surname
[Created] => 2016-08-01T10:15:25Z
[Version] => 14.00 )
[OfficeDocumentSettings] => SimpleXMLElement Object ( [AllowPNG] => SimpleXMLElement Object ( ) )
[ExcelWorkbook] => SimpleXMLElement Object ( [WindowHeight] => 9375 [WindowWidth] => 9705 [WindowTopX] => 270 [WindowTopY] => 615 [ProtectStructure] => False [ProtectWindows] => False )
[Styles] => SimpleXMLElement Object ( [Style] => SimpleXMLElement Object ( [Alignment] => SimpleXMLElement Object ( ) [Font] => SimpleXMLElement Object ( ) ) )
[Worksheet] => Array (
[0] => SimpleXMLElement Object (
[Table] => SimpleXMLElement Object (
[Column] => Array (
[0] => SimpleXMLElement Object ( )
[1] => SimpleXMLElement Object ( )
[2] => SimpleXMLElement Object ( ) )
[Row] => Array (
[0] => SimpleXMLElement Object (
[Cell] => Array (
[0] => SimpleXMLElement Object ( [Data] => Model )
[1] => SimpleXMLElement Object ( [Data] => Qty )
[2] => SimpleXMLElement Object ( [Data] => Description )
[3] => SimpleXMLElement Object ( [Data] => Tags ) ) )
[1] => SimpleXMLElement Object (
[Cell] => Array (
[0] => SimpleXMLElement Object ( [Data] => model_name )
[1] => SimpleXMLElement Object ( [Data] => 1 )
[2] => SimpleXMLElement Object ( [Data] => This is my item ) ) )
[....code repeats with other rows/worksheets....]
Now, if I want to display the Author name, I can simply do something like:
echo $xml->DocumentProperties->LastAuthor;
and I will get the expected result. How do I get to display the values in the various rows/cells (keeping in mind that the xml files will have a very diverse number of rows and rows are not always completely filled)?
I've tried something like the following:
echo $xml->Worksheet[0]->Table->Row[0]->Cell[0];
But doing so doesn't return any value. I would also like to be able to cycle through all the rows so that I can display them as an HTML table.
Any ideas?
Thanks a lot in advance for your help!
I need to group this array according to proposal_num, the array with same proposal number will lies in the same row. How can i do this.. the array structure and other details are below.
Array
(
[0] => Array
(
[proposal_num] => SimpleXMLElement Object
(
[0] => 1
)
[date_departure] => SimpleXMLElement Object
(
[0] => 150715
)
[time_departure] => SimpleXMLElement Object
(
[0] => 0713
)
[date_arrival] => SimpleXMLElement Object
(
[0] => 150715
)
[time_arrival] => SimpleXMLElement Object
(
[0] => 0845
)
[departure] => SimpleXMLElement Object
(
[0] => ATL
)
[depart_terminal] => SimpleXMLElement Object
(
[0] => N
)
[arrival] => SimpleXMLElement Object
(
[0] => BWI
)
[marketing_cmpny] => SimpleXMLElement Object
(
[0] => NK
)
[operating_cmpny] => SimpleXMLElement Object
(
[0] => NK
)
[flight_number] => SimpleXMLElement Object
(
[0] => 824
)
[equipment_type] => SimpleXMLElement Object
(
[0] => 319
)
[total_fare] => SimpleXMLElement Object
(
[0] => 326.20
)
[total_tax] => SimpleXMLElement Object
(
[0] => 49.00
)
)
[1] => Array
(
[proposal_num] => SimpleXMLElement Object
(
[0] => 2
)
[date_departure] => SimpleXMLElement Object
(
[0] => 150715
)
[time_departure] => SimpleXMLElement Object
(
[0] => 1535
)
[date_arrival] => SimpleXMLElement Object
(
[0] => 150715
)
[time_arrival] => SimpleXMLElement Object
(
[0] => 1702
)
[departure] => SimpleXMLElement Object
(
[0] => ATL
)
[depart_terminal] => SimpleXMLElement Object
(
[0] => N
)
[arrival] => SimpleXMLElement Object
(
[0] => CLT
)
[marketing_cmpny] => SimpleXMLElement Object
(
[0] => AA
)
[operating_cmpny] => SimpleXMLElement Object
(
[0] => US
)
[flight_number] => SimpleXMLElement Object
(
[0] => 1711
)
[equipment_type] => SimpleXMLElement Object
(
[0] => 321
)
[total_fare] => SimpleXMLElement Object
(
[0] => 430.20
)
[total_tax] => SimpleXMLElement Object
(
[0] => 69.28
)
)
[2] => Array
(
[proposal_num] => SimpleXMLElement Object
(
[0] => 2
)
[date_departure] => SimpleXMLElement Object
(
[0] => 150715
)
[time_departure] => SimpleXMLElement Object
(
[0] => 1740
)
[date_arrival] => SimpleXMLElement Object
(
[0] => 150715
)
[time_arrival] => SimpleXMLElement Object
(
[0] => 1903
)
[departure] => SimpleXMLElement Object
(
[0] => CLT
)
[arrival] => SimpleXMLElement Object
(
[0] => BWI
)
[marketing_cmpny] => SimpleXMLElement Object
(
[0] => AA
)
[operating_cmpny] => SimpleXMLElement Object
(
[0] => US
)
[flight_number] => SimpleXMLElement Object
(
[0] => 703
)
[equipment_type] => SimpleXMLElement Object
(
[0] => 321
)
[total_fare] => SimpleXMLElement Object
(
[0] => 430.20
)
[total_tax] => SimpleXMLElement Object
(
[0] => 69.28
)
)
)
i wanted to group this array according to proposal number.
i need output like..
Airline Stops Departs Arrives Duraton Price
NK ATL→BWI 15/07/15 07:13 15/07/15 08:45 1hr 32m $326.20
US ATL→CLT 15/07/15 15:35 15/07/15 17:02 1hr 27m $430.20
US CLT→BWI 15/07/15 17:40 15/07/15 19:03 1hr 23m
please help me to solve this .. thanks in advance
I have a problem to sort array of SimpleXMLElement Object. The data is:
[ServiceHotel] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[availToken] => 0
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[availToken] => 1
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[availToken] => 2
)
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[availToken] => 3
)
)
)
I want to sort that data, order by availToken in PHP. Can anybody help me?
Here is my Xml code:
SimpleXMLElement Object
(
[resultsRows] => SimpleXMLElement Object
(
[row] => Array
(
[0] => SimpleXMLElement Object
(
[dimension] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => date
[value] => 20140102
[label] => Date
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => browserType
[value] => Chrome
[label] => Browser Type
)
)
)
[metric] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => visitDuration
[value] => 1242
[label] => Avg. Visit Duration
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => bounces
[value] => 3
[label] => Bounces
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => repeatVisitors
[value] => 0
[label] => Repeat Visitors
)
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => newVisitors
[value] => 5
[label] => New Visitors
)
)
[4] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => visits
[value] => 10
[label] => Visits
)
)
[5] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => pageViews
[value] => 66
[label] => Page Views
)
)
)
)
)
)
)
Above Xml array need to print like key and values type.The array showing like dimension and metric.I want print the values inside #attributes nested array like key and value.
Thanks.
Found in the comments on php SimpleXml documentation:
function xml2array ( $xmlObject, $out = array () )
{
foreach ( (array) $xmlObject as $index => $node )
$out[$index] = ( is_object ( $node ) ) ? xml2array ( $node ) : $node;
return $out;
}
Check the docs here: http://cl1.php.net/ref.simplexml
I have a function foo that returns an array with the below structure and what I am trying to work out is there a better way to structure my PHP as I am unsure about the indexing within the foreach as I want every ListingId
Goal:
I am wanting to write a foreach loop that gets the XML <Name> from an external XML document (Have not coded this section yet as I need to pass the ListingID from foointo the url to get the <Name>)
PHP:
$test0 = $this->foo();
$test = $test0[0]['ListingId'];
Structure:
Array ( [0] => Array ( [ListingId] => SimpleXMLElement Object ( [0] => 532712629 ) [ListingCategory] => SimpleXMLElement Object ( [0] => 0350-5748-3400- ) ) [1] => Array ( [ListingId] => SimpleXMLElement Object ( [0] => 532712202 ) [ListingCategory] => SimpleXMLElement Object ( [0] => 0350-5748-3400- ) ) [2] => Array ( [ListingId] => SimpleXMLElement Object ( [0] => 532711566 ) [ListingCategory] => SimpleXMLElement Object ( [0] => 0350-5748-3400- ) ) [3] => Array ( [ListingId] => SimpleXMLElement Object ( [0] => 532710864 ) [ListingCategory] => SimpleXMLElement Object ( [0] => 0350-5748-3400- ) ) [4] => Array ( [ListingId] => SimpleXMLElement Object ( [0] => 532710271 ) [ListingCategory] => SimpleXMLElement Object ( [0] => 0350-5748-3400- ) ) [5] => Array ( [ListingId] => SimpleXMLElement Object ( [0] => 532691526 ) [ListingCategory] => SimpleXMLElement Object ( [0] => 0350-5748-3400- ) ) [6] => Array ( [ListingId] => SimpleXMLElement Object ( [0] => 527496168 ) [ListingCategory] => SimpleXMLElement Object ( [0] => 0350-5748-3399- ) ) )
Try this way:
foreach($this->foo() as $foo) {
//here you can use your ListingId
var_dump($foo['ListingId']);
}