Array help with multiple PHP levels?

Asked

Viewed 215 times

0

I’m getting this array:

Array
(
    [0] => {"Status": {"Alerta": ["Uma ou mais companhias n\u00e3o retornaram voos"], "Erro": false, "Sucesso": true
    [1] => Busca": {"Criancas": 0, "Adultos": 1, "TipoViagem": 0, "Trechos": [{"DataIda": "15/03/2018", "Destino": "GRU", "Origem": "VRN"}], "Chave": "b68e5504453bc415eb9617722c064c31c86141a0", "Senha": "2b66dde677ba97e2d75e82b1997d4c7e2d0aee65", "TipoBusca": 1, "Bebes": 0, "Companhias": ["gol", "latam", "azul"]
    [2] => Trechos": {"VRNGRU": {"Semana": {"16/03/2018": [{"Milhas": "", "Valor": "", "Companhia": "GOL"}, {"Milhas": 150000.0, "Valor": "", "Companhia": "LATAM"}], "18/03/2018": [{"Milhas": "", "Valor": "", "Companhia": "GOL"}, {"Milhas": "", "Valor": "", "Companhia": "LATAM"}], "13/03/2018": [{"Milhas": "", "Valor": "", "Companhia": "GOL"}, {"Milhas": "", "Valor": "", "Companhia": "LATAM"}], "15/03/2018": [{"Milhas": "", "Valor": "", "Companhia": "GOL"}, {"Milhas": "", "Valor": "", "Companhia": "LATAM"}], "17/03/2018": [{"Milhas": "", "Valor": "", "Companhia": "GOL"}, {"Milhas": "", "Valor": "", "Companhia": "LATAM"}], "12/03/2018": [{"Milhas": "", "Valor": "", "Companhia": "GOL"}, {"Milhas": "", "Valor": "", "Companhia": "LATAM"}], "14/03/2018": [{"Milhas": "", "Valor": "", "Companhia": "GOL"}, {"Milhas": "", "Valor": "", "Companhia": "LATAM"}]
    [3] => Voos": [], "Destino": "GRU", "Data": "15/03/2018", "Origem": "VRN"}}}
)

and would need to access each of them separately,

Example: Capture

Busca -> Crianças (e todos os seus dados separados)

Trechos -> Semana (e todos os seus dados separados)

How best to achieve this result?

  • The array is just like that, it seems to me that keys are missing ... ?

  • This array is not valid think.... use is_array($seuArray) to verify...

  • Good really it is not an array, as I can convert this value, to an array?

  • Probably an array of json... mixing !

1 answer

0

Possibly this is a JSON response, use the function json_decode() to convert this to a PHP array. Example:

<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json));
var_dump(json_decode($json, true));

?>
  • Right, he returned me as follows: Object(stdClass)#3 (3) { ["Status"]=> Object(stdClass)#4 (3) { ["Alert"]=> array(0) { } ["Error"]=> bool(false) ["Success"]=> bool(true) } ["Search"]=> Object(stdClass)#5 (9) { ["Children"]=> int(0) ["Adults"]=> int(1) ["Typing"]=> int(0) ["Excerpts"]=> array(1) { [0]=> Object(stdClass)#6 (3) { ["Dated"]=> string(10) "15/03/2018" ["Target"]=> string(3) "GRU" ["Origin"]=> string(3) "GIG" } }...

  • You returned what you wanted just right. The function documentation shows that second parameter, to return with array and only use the second false parameter

  • Excuse my inexperience in array, how can I make it become an array, so I can capture the data I need?

  • It is already an array, only has to navigate its dimensions, like using multiple brackets $var[0][2][4]["Destination"]

  • Type cannot navigate like this: print_r($string[0][0[2]]); OU for($string as $result){ echo $result[0][1][2]; }

Browser other questions tagged

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