2

I am a bit of a newbie to PHP and I'm not sure what I'm missing here. I have an multidimensional array that I've created from an XML file using XPath. I'm able to move through the array and retrieve most all values but I am getting stuck on one section.

Example of XML structure:

  • MasterNode
    • SubNodeItem1
    • SubNodeItem2
    • SubNodeItem3
    • SubNodeItemList
      • SubListItem
        • SubItemProperty1
        • SubItemProperty2
        • SubItemProperty3
      • SubItemList
        • SubItemProperty1
        • SubItemProperty2
        • SubItemProperty3
    • SubNodeItem4
    • SubNodeItem5

I am able to retrieve the value of any of the SubNode values by using the following syntax:

$val=$XML[$i]->SubNodeItem1; 

however, I can not for the life of me figure out how to retrieve the values of SubListItemProperty.

I figured this would be the logical syntax:

$SubItemPropVal=$XML[$i]->SubNodeItemList->SubListItem[$i]->SubItemProperty1; 

I have searched other forums and topics related to PHP multi arrays and have not been able to find the proper way to do this.

I am getting a "Trying to get property of non-object" error when I run the code. I'm pretty sure that's the indication that I'm not pointing the node correctly.

2
  • Question number one: You already have an XML file with all the flexibility XPath gives you. Why would you want to give that up and use an array instead? Commented Dec 13, 2012 at 17:32
  • Do you actually have an multi-dimensional array populated from XML, or are you dealing with an XML object representation? If a multi-dimensional array you would not be using object access notation (->) at all. Please provide var_dump of your object in the question, it will be much more informative. Commented Dec 13, 2012 at 17:32

1 Answer 1

1

My recommendation would be to keep the XML file, which apparently works fine already, and use it.

Transferring its elements into an array does not make much sense to me.

EDIT: The OP does not actually use an array, but a SimpleXML object.

XPath is extremely flexible and powerful in selecting the needed bits from an XML document:

$doc = new DOMDocument(); $doc->loadXML($your_xml); $xp = new DOMXPath($doc); // for example $result = $xp->query("//SubListItem[2]/SubItemProperty1"); if ($result->length) { echo $result->item(0)->textContent; } 

SimpleXML would also work:

$xml = simplexml_load_string($result); // either this ($node will be an array of matches, or FALSE) $node = $xml->xpath("//SubNodeItemList/SubListItem[1]/SubItemProperty1"); // or this (unless you add a number, [0] will be assumed) $node = $xml->SubNodeItemList->SubListItem->SubItemProperty1; 

Important: Array notation counts from 0, while XPath always counts from 1.

Note that the second option (array notation) will throw run-time errors when the structure of the document is not what your code expects.

With XPath there would simply be no return value, which is easier to handle (no try/catch block necessary, an if ($node) { ... } suffices).

Also note that with SimpleXML, the document element (<MasterNode>) becomes the document. So you would not use $xml->MasterNode->SubNodeItemList, but $xml->SubNodeItemList.

Sign up to request clarification or add additional context in comments.

5 Comments

Well First all let me say WOW, this forum is impressive. So much helpful information within 5 minutes of posting! First I suppose my understanding of xpath was a little off. I don't believe I have an array but instead i have an xpath object created by using: $xml = simplexml_load_string($result); ($result is a request recieved from an http post). I think I understand your suggestion but in order to retrieve the SubItemProperty wouldn't I actually need to use: <br> $node = $xp->query("//SubNodeItemList/SubListItem[2]/SubItemProperty1");?
Ah, you're using SimpleXML, that was not obvious. (Note that you can use backticks to format inline code.) I'll add an example for SimpleXML to my answer. Your XPath looks fine, too. They'd both work, mine is just less specific. Of course more specific is better.
Awesome, thank you! I'm curious to see your example as I can't find the right way to do this anywhere.
O.K. Here's is what was happening. I did assume the document element would become the actual document and my original xpath was reflecting that. My error was that I hadn't looked at the var_dump to realize that the HTTP response was being included as a top level node... so I actually needed TO include that node in my XPATH. I'm now able to retrieve all the values I'm looking for, BUT..... I am now getting another error when trying to put the values I need to save into an array-"Illegal offset type in.." is the error I'm getting. I can echo out the value of the variable just fine. Any idea?
SORRY for the premature question. I figured out that I needed to convert to a string before saving into the array. THANKS everyone!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.