0

I'm relatively new to PHP and I'm working in this PHP project. I'm looping through an array of SimpleXMLElement objects that look like this:

SimpleXMLElement
{
RunID : "321",
Description: "Something Here"
}

Note that this array came from a SOAP call that returns XML.

All I want to do is rename the properties RunID and Description to something else and add each element to a new array. Some help would be greatly appreciated.

If its not possible to rename them, then can I convert them to another object type or something. Like I could do with an anonymous type in C#. I'm using PHP 5.6

Jesus
  • 318
  • 1
  • 4
  • 17
  • Is this a var_dump/print_r of a simpleXMLElement object? Doesn't seem like it is. Correct me if i am wrong. – Andreas Sep 26 '16 at 17:50
  • I used simplexml_load_string to load the XML and turn into an SimpleXMLElement. Then I have SimpleXMLElement that has an array of SimpleXMLElement with the properties I mentioned above. No its not a var_dump, Its just a representation of the SimpleXMLElement that I have. – Jesus Sep 26 '16 at 17:53
  • Check the answer here, It seems to show how to convert the result of **simplexml_load_string(...)** to regular php array: http://stackoverflow.com/questions/6167279/converting-a-simplexml-object-to-an-array#6167346 – mikhail-t Sep 26 '16 at 18:48

1 Answers1

0

So I finally figured out. All I had to do is the following. I hope it helps someone.

$myArray = array();
foreach($xml as $element)
{
$row["NewPropertyName"] = (string)$element->RunID;
$row["SecondPropertyName"] = (string)$element->Description;
$myArray[] = $row;
}
Jesus
  • 318
  • 1
  • 4
  • 17
  • You should probably add `$row = array();` above `$row["NewPropertyName"]` so that you're creating a new `$row` array each time around the loop. If your code gets more complicated later, you may encounter bugs if you forget to add this in. – IMSoP Sep 27 '16 at 09:04