php - How to push xml object to array? -
i want push each coa array.here xml file code.
<?xml version="1.0" encoding="utf-8"?> <companies> <company> <name>best western</name> <groups> <group> <coa>410101 · net room revenue taxable</coa> <coa>415101 · gtd no show</coa> <coa>total 400000 · room revenue</coa> <coa>total · room revenue</coa> <coa>ii · other operating revenue</coa> <coa>425120 · meeting room</coa> <coa>480380 · interest income</coa> <coa>480383 · guest laundry</coa> <coa>480385 · vending</coa> <coa>480389 · miscellaneous</coa> <coa>482000 · sales tax discounts</coa> <coa>total 480000 · other income</coa> <coa>total ii · other operating revenue</coa> <coa>total income</coa> </group> <group> <coa>10 · rooms department</coa> <coa>total income</coa> </group> </groups> </company> </companies>
now have tried code
$xml = simplexml_load_file($url); foreach ($xml $company) { $groups = $company->groups; $coa_list = $groups->group; $coa_array = array(); foreach ($coa_list $coal) { $coa_array = array(); $coa_k = $coal->coa; foreach ($coa_k $key => $value) { $val = $value; array_push($coa_array, $val); } } }
actually should work dont know why not working.
note: reading xml file.
when print $val before array push, print cannot push array.
so can suggest me issue code
thanks
file example.xml
<?xml version="1.0" encoding="utf-8"?> <companies> <company> <name>best western</name> <groups> <group> <coa>410101 · net room revenue taxable</coa> <coa>415101 · gtd no show</coa> <coa>total 400000 · room revenue</coa> <coa>total · room revenue</coa> <coa>ii · other operating revenue</coa> <coa>425120 · meeting room</coa> <coa>480380 · interest income</coa> <coa>480383 · guest laundry</coa> <coa>480385 · vending</coa> <coa>480389 · miscellaneous</coa> <coa>482000 · sales tax discounts</coa> <coa>total 480000 · other income</coa> <coa>total ii · other operating revenue</coa> <coa>total income</coa> </group> <group> <coa>10 · rooms department</coa> <coa>total income</coa> </group> </groups> </company> </companies>
here php code fetching coa have convert simplexmlobject array.
<?php $xml = simplexml_load_file('example.xml'); // echo "<pre>"; // print_r($xml); // echo "</pre>"; $coa_array = array(); foreach ($xml $company) { $groups = $company->groups; $coa_list = $groups->group; foreach ($coa_list $coal) { $coa_k = $coal->coa; foreach ($coa_k $key => $value) { $val = xml2array($value); // echo "<pre>"; // print_r($val[0]); // echo "</pre>"; // $val[] array_push($coa_array, $val[0]); } } } echo "<pre>"; print_r($coa_array); echo "</pre>"; function xml2array( $xmlobject, $out = array () ){ foreach ( (array) $xmlobject $index => $node ) $out[$index] = ( is_object ( $node ) ) ? xml2array ( $node ) : $node; return $out; } ?>
result be
array ( [0] => 410101 · net room revenue taxable [1] => 415101 · gtd no show [2] => total 400000 · room revenue [3] => total · room revenue [4] => ii · other operating revenue [5] => 425120 · meeting room [6] => 480380 · interest income [7] => 480383 · guest laundry [8] => 480385 · vending [9] => 480389 · miscellaneous [10] => 482000 · sales tax discounts [11] => total 480000 · other income [12] => total ii · other operating revenue [13] => total income [14] => 10 · rooms department [15] => total income )
Comments
Post a Comment