How to print array values

Asked

Viewed 129 times

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>

Exit:

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
                                )

                        )

                )

        )

)

1 answer

3


This is not an array, it is an object Simplexmlelement which can be iterated as an array, but not through the same square bracket notation as an.

Since I don’t 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 input data reply, use as a foreach argument:

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

    // Use $nodes -> paginacao
}

If you actually want to work with this data as an array, you can convert this Simplexmlelement object to an array with a nice little 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 );

Being $sxml the variable with the Simplexmlelement object.

But this is not at all recommendable because you no longer have access to Simplexml resources and can not, if necessary, import the object in question to the GIFT which offers a much wider range of features to work with.

  • added the xml file in the post

  • I kicked the syntax, but that’s right.

  • and how to use this second function?

  • Ooops, missed it right? I’ll add, but it’s like any other function. ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.