How to print values of this type of array

1

This is the xml used to generate the array

 <?xml version="1.0" encoding="utf-8"?>
    <integracao>
        <status>2</status>
        <resposta>
            <paginacao>
                <totalItens>2</totalItens>
                <paginaAtual>1</paginaAtual>              
                <registrosPorPagina>10</registrosPorPagina>        
                <ultimaPagina>1</ultimaPagina>
            </paginacao>
            <historico>
                <registro>
                    <transacao>19569951</transacao>
                    <email></email>
                    <valor>2000</valor>        
                    <dataEmissao>2014-09-02T12:09:14</dataEmissao>       
                    <status>aguardando</status>
                    <codigoStatus>1</codigoStatus>
                </registro>
                <registro>
                    <transacao>19561474</transacao>
                    <email></email>
                    <valor>2000</valor> 
                    <dataEmissao>2014-09-01T01:09:20</dataEmissao>  
                    <status>aguardando</status>
                    <codigoStatus>1</codigoStatus>
                </registro>
            </historico>
        </resposta>
    </integracao>

Output:

SimpleXMLElement Object
(
    [status] => 2
    [resposta] => SimpleXMLElement Object
        (
            [paginacao] => SimpleXMLElement Object
                (
                    [totalItens] => 4
                    [paginaAtual] => 1
                    [registrosPorPagina] => 10
                    [ultimaPagina] => 1
                )

            [historico] => SimpleXMLElement Object
                (
                    [registro] => Array
                        (
                            [0] => SimpleXMLElement Object
                                (
                                    [transacao] => 19605633
                                    [email] => [email protected]
                                    [valor] => 500
                                    [dataEmissao] => 2014-09-04T01:09:25
                                    [status] => aguardando
                                    [codigoStatus] => 1
                                )

                            [1] => SimpleXMLElement Object
                                (
                                    [transacao] => 19605619
                                    [email] => email_cliente
                                    [valor] => 500
                                    [dataEmissao] => 2014-09-04T01:09:14
                                    [status] => cancelado
                                    [codigoStatus] => 8
                                )

                            [2] => SimpleXMLElement Object
                                (
                                    [transacao] => 19569951
                                    [email] => SimpleXMLElement Object
                                        (
                                        )

                                    [valor] => 2000
                                    [dataEmissao] => 2014-09-02T12:09:14
                                    [status] => aguardando
                                    [codigoStatus] => 1
                                )

                            [3] => SimpleXMLElement Object
                                (
                                    [transacao] => 19561474
                                    [email] => SimpleXMLElement Object
                                        (
                                        )

                                    [valor] => 2000
                                    [dataEmissao] => 2014-09-01T01:09:20
                                    [status] => aguardando
                                    [codigoStatus] => 1
                                )

                        )

                )

        )

)
    
asked by anonymous 04.09.2014 / 20:41

1 answer

3

This is not an array, it's a SimpleXMLElement object which can be iterated like an array, but not through it bracket notation than one.

As I do not have the same XML as you to offer you a 100% accurate solution, try something like:

echo $sxml -> status; // Deve retornar 2

If you need the input data response , use as a foreach argument:

foreach( $sxml -> resposta as $nodes ) {

    // Use $nodes -> paginacao
}

If you really want to work with this data as an array, you can convert this SimpleXMLElement object to an array with a pretty cool function:

function map( $param ) {

    if( is_object( $param ) ) {
        $param = get_object_vars( $param );
    }

    if( is_array( $param ) ) {
        return array_map( __FUNCTION__, $param );
    }

    return $param;
}

$array = map( $sxml );

$ sxml is the variable with the SimpleXMLElement object.

But this is not recommended because you no longer have access to the features of SimpleXML and can not, if necessary, import the object in question to DOM which offers a much larger range of features to work with.

    
04.09.2014 / 21:02