简体   繁体   中英

Parsing XML attributes and values into PHP associative array

Given XML with the following format

<?xml version="1.0"?>
<table>
 <row id="515" employeeId="7">
  <field id="startDate">2016-03-28</field>
  <field id="rate" currency="USD">15.00</field>
  <field id="type">Hourly</field>
  <field id="exempt"></field>
  <field id="reason"></field>
  <field id="comment"></field>
  <field id="paidPer">Hour</field>
  <field id="paySchedule">Monthly</field>
 </row>
 <row id="1917" employeeId="7">
  <field id="startDate">2017-09-11</field>
  <field id="rate" currency="USD">2500.00</field>
  <field id="type">Salary</field>
  <field id="exempt">Exempt</field>
  <field id="reason">Equity Adjustment</field>
  <field id="comment">Increase for 2017 performance</field>
  <field id="paidPer">PayPeriod</field>
  <field id="paySchedule">Every other week</field>
 </row>
</table>

$node3 = simplexml_load_string($prates, "SimpleXMLElement", LIBXML_NOCDATA);
foreach($node3->row as $r) {
            for($i=0;$i<count($r);$i++) {
            var_dump($r->field[$i]["id"][0]);
            }

returns my list of id attributes for my XML.
Given that the usual method of encode JSON/decode XML does not preserve the attributes, and what I ultimately want is an array that looks like

$rates[0][first id] => value of this field

$rates[0][second id] => value of this field

and if there is a second row in the XML

$rates[1][first id] => value of this field

etc so that I can then sort the array by startDates to find the most recent row. Is there an easier way to do this? I feel like I have been looking at this for too long...

You can do a double loop: one for each row, and a second for each field in the current row:

$rates = array() ; // store data of the whole document
foreach($node3 as $row) // loop over each row
{
    $rowData = array() ; // store the current row data
    foreach($row->field as $field) // loop over each field
    {
        $id = (string) $field->attributes()['id'] ; // retrieve the 'id' attribute
        $value = (string) $field ; // retrieve the text inside the node

        $rowData[$id] = $value ; // add the id/value pair
    }

    $rates[] = $rowData ; // add the row data to the global array
}

print_r($rates);

Output:

Array
(
[0] => Array
    (
        [startDate] => 2016-03-28
        [rate] => 15.00
        [type] => Hourly
        [exempt] => 
        [reason] => 
        [comment] => 
        [paidPer] => Hour
        [paySchedule] => Monthly
    )

[1] => Array
    (
        [startDate] => 2017-09-11
        [rate] => 2500.00
        [type] => Salary
        [exempt] => Exempt
        [reason] => Equity Adjustment
        [comment] => Increase for 2017 performance
        [paidPer] => PayPeriod
        [paySchedule] => Every other week
    )
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM