0

I have an XML file that was converted into an array with xpath. Ultimately I want to be able to access the variables in the array but no method seems to work.

echo $items['member']['name']; Does not work
echo $items['name']; Nor this
echo $items['members']['member']['name']; Nor this
echo $items[0][0] Nor this
echo $items[0] Nor this
echo $items[0]['object']['name']Nor this
$items[0]['name']Nor this

I have no no what the problem is but I imagine it might be with xpath and it's effect on the array. I understand that there are other similar threads but no resource seems to help.

PHP:

    $xml = simplexml_load_file('XML/profiles.xml'); //Create XML variable from XML file.

    $items = $xml->xpath('member');

    var_dump($items);
    ?>

XML:

    <member>
        <name>AndrewHarvey</name>
        <location>Carleton-Victoria</location>
        <party>L</party>
    </member>

    <member>
        <name>BenoitBourque</name>
        <location>Kent North</location>
        <party>L</party>
    </member>

    <member>
        <name>BernardLeBlanc</name>
        <location>Memramcook-Tantramar</location>
        <party>L</party>
    </member>

    <member>
        <name>BertrandLeBlanc</name>
        <location>Kent North</location>
        <party>L</party>
    </member>

    <member>
        <name>BillFraser</name>
        <location>Miramichi</location>
        <pos>Tourism</pos>
        <pos>Heritage and Culture</pos>
        <pos>Northern and Miramichi Funds</pos>
        <party>L</party>
    </member>
and so on...

Var dump of $array:

array (size=49)  
      0 =>   
      object(SimpleXMLElement)[2]  
      public 'name' => string 'AndrewHarvey' (length=12)  
      public 'location' => string 'Carleton-Victoria' (length=17)  
      public 'party' => string 'L' (length=1)  
  1 =>   
    object(SimpleXMLElement)[3]  
      public 'name' => string 'BenoitBourque' (length=13)  
      public 'location' => string 'Kent North' (length=10)  
      public 'party' => string 'L' (length=1)  
  2 => 
    object(SimpleXMLElement)[4]
      public 'name' => string 'BernardLeBlanc' (length=14)
      public 'location' => string 'Memramcook-Tantramar' (length=20)
      public 'party' => string 'L' (length=1)
  3 => 
    object(SimpleXMLElement)[5]
      public 'name' => string 'BertrandLeBlanc' (length=15)
      public 'location' => string 'Kent North' (length=10)
      public 'party' => string 'L' (length=1)
  4 => 
    object(SimpleXMLElement)[6]
      public 'name' => string 'BillFraser' (length=10)
      public 'location' => string 'Miramichi' (length=9)
      public 'pos' => 
        array (size=3)
          0 => string 'Tourism' (length=7)
          1 => string 'Heritage and Culture' (length=20)
          2 => string 'Northern and Miramichi Funds' (length=28)
and so on...
BrandonFlynn-NB
  • 384
  • 2
  • 14

3 Answers3

1

Your array is an Object array, so you need the -> to access them. Using the foreach you can access the array.

foreach($items as $val){
    echo $val->name;
    echo $val->location;
    echo $val->party;
}

Try this and let me know. If anything else in the array, you need to do it similar way.

Murad Hasan
  • 9,565
  • 2
  • 21
  • 42
1

To add to the comment Frayne Konok made you could access those values like this: $name = array[0] -> name;

You could also loop through each of them as:

foreach ($node as $index => $data)
{
   $name = $data -> name;
   $location = $data -> location;

   if (property_exists($data, 'party'))
   { $party = $data -> party; }       

   if (property_exists($data, 'pos'))
   { $pos_attr = $data -> pos[0]; }
}

Little disclaimer here... I didn't test this so you would want to play around with this, but you get the idea.

The array at the top of your var_dump there denotes that the outer wrapping is an array and would need to be accessed like one. The object(SimpleXMLElement) underneath each of those entries denotes that the items in that array are objects and would have to be accessed as such. The array (size=3) under the poc attribute denotes that it's an array. You can see where I'm going with this. Arrays are accessed using bracket notation array[0] or array['example'] and objects are accessed using arrow notation object -> name or object -> {'0'} (in odd cases).

Robert McMahan
  • 531
  • 5
  • 6
0

SimpleXMLElement::xpath() always returns an array of SimpleXMLElement objects. It is not a multidimensional or nested array. However SimpleXMLElement implements a lot of syntax sugar. One of that is array syntax for attributes.

$element = new SimpleXMLElement('<foo attr="bar"/>');
echo $element['attr'];

But to access child nodes, you need to use object syntax.

$xml = simplexml_load_file('XML/profiles.xml');

foreach ($xml->xpath('member') as $member) {
  echo $member->name;
}

The var_dump() output of a SimpleXMLElement is not a full/correct representation of the actual object, because of the syntax sugar. For example you can iterate the SimpleXMLElement properties, too.

$xml = simplexml_load_file('XML/profiles.xml');

foreach ($xml->member as $member) {
  echo $member->name;
}
ThW
  • 19,120
  • 3
  • 22
  • 44