phparraysobjectsimplexmltranspose

Transpose a SimpleXMLElement object containing arrays


I have this array:

SimpleXMLElement Object
(
[id] => Array
    (
        [0] => Koala.jpg
        [1] => Jellyfish.jpg
    )

[desc] => Array
    (
        [0] => koaladesc
        [1] => jelly desc
    )

[qtidade] => Array
    (
        [0] => 1
        [1] => 5
    )

I need create some php function that help me group the values like this:

[0] => Array
    (
        [0] => Koala.jpg
        [1] => koaladesc
        [2] => 1
    )

[1] => Array
    (
        [0] => Jellyfish
        [1] => jelly desc
        [2] => 5
    )

Could anyone help me?


Solution

  • Something like this should do the trick, but it's localized to what you're asking based on the vagueness of your question:

    $new_array = array();
    foreach($simple_xml_object as $obj) {
        if(is_array($obj)) {
            for($i = 0; $i < count($obj); $i++) {
                $new_array[$i][] = $obj[$i];
            }
        }
    }
    

    I would suggest looking at the documentation on the foreach() construct, as well as looking over the SimpleXML manual.