2

I do receive XML results from a web service which I read as SimpleXML elements. Now there's the situation that results can differ depending on the configuration delivered by the web service.

The point is: I'd like to wrap an array around the SimpleXML object in "Situation 2" so I do not have to differentiate between "config as SimpleXML object inside array" and "config as SimpleXML object" when processing the data later on. I've been looking&thinking for a solution for some time, but as of now I couldn't figure anything out.

Situation 1: Multiple SimpleXML Elements inside an array are returned

["myConfig"]=>
array(2) {
  [0]=>
  object(SimpleXMLElement)#41 (5) {
    ["id"]=>
    string(1) "1"
    ["type"]=>
    string(1) "4"
    ["comment"]=>
    string(2) "foobar"
    ["name"]=>
    string(1) "test"
    ["attribute"]=>
    string(5) "value"
  }
  [1]=>
  object(SimpleXMLElement)#42 (5) {
    ["id"]=>
    string(1) "4"
    ["type"]=>
    string(1) "2"
    ["comment"]=>
    string(8) "twobar"
    ["name"]=>
    string(10) "test2"
    ["attribute"]=>
    string(5) "value"
  }
}

Situation 2: Config only contains one SimpleXML element, no array returned by web service

 ["myConfig"]=>
object(SimpleXMLElement)#41 (5) {
  ["id"]=>
  string(1) "1"
  ["type"]=>
  string(1) "4"
  ["comment"]=>
  string(2) "foobar"
  ["name"]=>
  string(1) "test"
  ["attribute"]=>
  string(5) "value"
}

Target for situation 2: I would like to create the following out of "Situation 2" on server-side, before continuing to work with the configuration data:

["myConfig"]=>
array(1) {
  [0]=>
  object(SimpleXMLElement)#41 (5) {
    ["id"]=>
    string(1) "1"
    ["type"]=>
    string(1) "4"
    ["comment"]=>
    string(2) "foobar"
    ["name"]=>
    string(1) "test"
    ["attribute"]=>
    string(5) "value"
  }
}
1
  • 1
    Check whether myConfig is an array, and if not simply make it one … Commented Apr 28, 2014 at 12:47

2 Answers 2

1

I managed to create and array out of the SimpleXML object this way. It's working fine for the moment, but this way I have to convert my SimpleXML object and continue with a PHP array:

// The SimpleXML Object $myConfig is converted to a PHP array
$fooConfig = json_decode(json_encode((array)$myConfig), TRUE);

// Check if ['id'] is a key in ['myConfig'], go through array if true
// and shift key => value pairs as array inside array
if(array_key_exists('id', $fooConfig['myConfig'])) {
    foreach($fooConfig['myConfig'] as $conKey => $conVal) {
        $fooConfig['myConfig'][0][$conKey] = array_shift($fooConfig['myConfig']);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

This sounds rather trivial:

$test = $fooConfig['myConfig'];

if (is_object($test) && $test instanceof SimpleXMLElement) {
    unset($fooConfig['myConfig']);
    $fooConfig['myConfig'] = array($test);
}

3 Comments

It doesn't work for me that way. When using your code in PHP 5.3 I get an error: Warning: It is not yet possible to assign complex types to properties in getConfig.php on line 70
Right, you need to unset $fooConfig['myConfig'] first before you can set it again in that case. A little oversight of mine, see the edit.
I think this still isn't working, at least not with SimpleXML. Simply assigning an XML object into the XML structure unfortunately doesn't work (same error). Maybe with a loop, I'll try that out tomorrow. But in general, you gave a nice solution to wrap an array around the object as I requested. Now it's more about putting it back to the right place. Maybe SimpleXML isn't the right thing to work with here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.