How to mount foreach to pick array values

Asked

Viewed 686 times

0

Good afternoon. How should I ride the foreach to take the values:

  • Barcode;
  • link;
  • Charge.

of this array:

array (
  'code' => 200,
  'data' => 
  array (
    'barcode' => '00000.00000 00000.000000 00000.000000 0 00000000000000',
    'link' => 'https://visualizacaosandbox.gerencianet.com.br/emissao/196843_16_MEHZE5/A4XB-AAAAAA-AAAAAA-AAAAAA',
    'pdf' => 
    array (
      'charge' => 'https://download.gerencianet.com.br/196843_16_MEHZE5/196843-AAAAAA-AAAAAA.pdf?sandbox=true',
    ),
    'expire_at' => '2019-09-04',
    'charge_id' => 758277,
    'status' => 'waiting',
    'total' => 10000,
    'payment' => 'banking_billet',
  ),
)

PHP

foreach ($row_rs["dataBoleto"]["data"] as $row) {
  echo $row["code"];
}

Thank you.

var_dump($row_rs);

array(20) { ["ID_Receita"]=> string(2) "20" ["id_cadastro"]=> string(2) "11" ["nome"]=> string(20) "Aluno de teste teste" ["matricula"]=> string(14) "70-47064508-19" ["parcela"]=> string(6) "1 de 4" ["planoDeConta"]=> string(5) "1.1.3" ["valor"]=> string(6) "175.00" ["dataLanc"]=> string(10) "2019-09-03" ["dataVenci"]=> string(10) "2019-10-03" ["dataPago"]=> NULL ["contaBancaria"]=> string(1) "2" ["formaEntrada"]=> string(1) "6" ["obs"]=> NULL ["cheqNomeProprietario"]=> NULL ["cheqBanco"]=> NULL ["cheqAgencia"]=> NULL ["cheqConta"]=> NULL ["cheqNumero"]=> NULL ["cheqDataDeposito"]=> NULL ["dataBoleto"]=> string(409) "{"code":200,"data":{"barcode":"00000.00000 00000.000000 00000.000000 0 00000000000000","link":"https://visualizacaosandbox.gerencianet.com.br/emissao/196843_16_MEHZE5/A4XB-AAAAAA-AAAAAA-AAAAAA","pdf":{"charge":"https://download.gerencianet.com.br/196843_16_MEHZE5/AAAAAA-AAAAAA-AAAAAA.pdf?sandbox=true"},"expire_at":"2019-09-04","charge_id":758277,"status":"waiting","total":10000,"payment":"banking_billet"}}" }

  • Tried so: echo $row["barcode"];

1 answer

2


You don’t need the foreach for that reason.

$data    = $row_rs["dataBoleto"]["data"];
$barcode = $data["barcode"];
$link    = $data["link"];
$charge  = $data["pdf"]["charge"];

If you need to go through several items within $row_rs, you will need:

foreach($row_rs as $dataBoleto) {
    $data    = $dataBoleto["data"];
    $barcode = $data["barcode"];
    $link    = $data["link"];
    $charge  = $data["pdf"]["charge"];
}

You just need to go through the sub-arrays inside your row.


Updating

In his var_dump, the index "dataBoleto" contains a JSON string inside:

["dataBoleto"]=> string(409)

Therefore, you should convert before to a notable object before working with it. In the example, it would look more or less like this:

$dataParsed = json_decode($row_rs["dataBoleto"], true /* para Array */);

$data    = $dataParsed["data"];
$barcode = $data["barcode"];
$link    = $data["link"];
$charge  = $data["pdf"]["charge"];

And then inside $dataParsed, will have the objects as a array and not a string.

  • Hello, thanks for the help. This error is showing Warning: Illegal string offset 'data' in

  • @James the array you put in status belongs to which object?

  • The whole array is within the $row_rs["dataBoleto"]

  • Strange, because in the example, $row_rs["dataBoleto"] contains the index data, which points to the rest of the content. It places var_dump of row_rs.

  • I updated the code

  • @James updated the answer.

Show 2 more comments

Browser other questions tagged

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