Problem handling foreach in a PHP array

Asked

Viewed 25 times

-2

I’m trying to go through the transaction of the following array but without success:

Array
(
    [date] => 2021-08-07T20:48:14.000-03:00
    [transactions] => Array
        (
            [transaction] => Array
                (
                    [date] => 2021-08-07T19:14:44.000-03:00
                    [reference] => TPW-253961
                    [code] => 3ECE880C-AEFF-4A3E-44E5
                    [type] => 1
                    [status] => 4
                    [paymentMethod] => Array
                        (
                            [type] => 11
                        )

                    [grossAmount] => 3.00
                    [discountAmount] => 0.00
                    [feeAmount] => 0.06
                    [netAmount] => 2.94
                    [extraAmount] => 0.00
                    [lastEventDate] => 2021-08-07T19:15:59.000-03:00
                )
            [transaction] => Array
                (
                    [date] => 2021-08-07T19:15:44.000-03:00
                    [reference] => TPW-253961
                    [code] => 3ECE880C-AEFF-4A3E-99EA
                    [type] => 1
                    [status] => 4
                    [paymentMethod] => Array
                        (
                            [type] => 11
                        )

                    [grossAmount] => 3.00
                    [discountAmount] => 0.00
                    [feeAmount] => 0.06
                    [netAmount] => 2.94
                    [extraAmount] => 0.00
                    [lastEventDate] => 2021-08-07T19:15:59.000-03:00
                )

        )

    [resultsInThisPage] => 1
    [currentPage] => 1
    [totalPages] => 1
)

The array is originally received from an XML, so I am converting as follows to PHP:

$xml = simplexml_load_string($response, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json,true);

And with the following foreach I’m trying to show off the status and code of each transaction:

foreach ($array["transactions"]["transaction"] as $resultado){
    echo $resultado["status"].' - '.$resultado["code"].'<br>';
}

The result of echo is something completely different than expected, besides not returning the status and code of each transaction, there are 12 result lines (one for each item in the transaction array:

inserir a descrição da imagem aqui

1 answer

0


I was able to solve by changing the parameter false for true in the array conversion.

$array = json_decode($json,true);

Instead of using foreach, I started using the for as follows:

for ($i = 0; $i < $array["resultsInThisPage"] OR $array["resultsInThisPage"] == 0 AND $i == 0; $i++){
    if ($array["resultsInThisPage"] < 2){
        $status = $array["transactions"]["transaction"]["status"];
        $code = $array["transactions"]["transaction"]["code"];
    } else {
        $status = $array["transactions"]["transaction"][$i]["status"];
        $code = $array["transactions"]["transaction"][$i]["code"];
    }
    echo $status.' - '.$code.'<br>';
}

In this way I was able to obtain the values of $status and $code in the array.

Browser other questions tagged

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