There is a json string that I decode into a multidimensional array:

[ { "@airline": "airBaltic", "@airline_code": "BT", "@title": "Из Санкт-Петербурга в Европу от 75 €! Специальное предложение от авиакомпании Air Baltic", "@id": "21994", "@href": "https://www.aviasales.ru/offers/iz-sankt-peterburga-v-evropu-ot-75-eur-spetsialnoe-predlozhenie-ot-aviakompanii-air-baltic-35f7648f-db47-41fd-a6aa-ce994621d1af", "@sale_date_begin": "1477526400", "@sale_date_end": "1477872000", "@flight_date_begin": "1477958400", "@flight_date_end": "1482105600", "@link": "https://hydra.aviasales.ru/adaptors/special_offer?iata=BT&utm_source=www&locale=ru&url=https%3A%2F%2Fwww.airbaltic.com%2Fru-ZZ%2Fcampaign%2Fbaltic-incoming-sale%3F", "description": [], "conditions": "...", "route": [ { "@from_iata": "LED", "@to_iata": "TLL", "@from_name": "Санкт-Петербург", "@to_name": "Таллин", "@oneway_price": "от 75 €", "@roundtrip_price": "" }, { "@from_iata": "LED", "@to_iata": "PLQ", "@from_name": "Санкт-Петербург", "@to_name": "Клайпеда", "@oneway_price": "от 75 €", "@roundtrip_price": "" }, { "@from_iata": "LED", "@to_iata": "VNO", "@from_name": "Санкт-Петербург", "@to_name": "Вильнюс", "@oneway_price": "от 75 €", "@roundtrip_price": "" }, { "@from_iata": "LED", "@to_iata": "RIX", "@from_name": "Санкт-Петербург", "@to_name": "Рига", "@oneway_price": "от 89 €", "@roundtrip_price": "" } ] } ] 

So, I need to refer to the first route subarray to the from_name element and get its value by key. In theory, the first route sub-array should have an index of 0, but when I try to access it, I get an empty string.

 <?php echo $offer['route'][0]['from_name'];?> 

Am I mistreating the first array?

var_dump ():

 object(SimpleXMLElement)#3 (4) { ["@attributes"]=> array(10) { ["airline"]=> string(9) "airBaltic" ["airline_code"]=> string(2) "BT" ["title"]=> string(152) "Из Санкт-Петербурга в Европу от 75 €! Специальное предложение от авиакомпании Air Baltic" ["id"]=> string(5) "21994" ["href"]=> string(159) "https://www.aviasales.ru/offers/iz-sankt-peterburga-v-evropu-ot-75-eur-spetsialnoe-predlozhenie-ot-aviakompanii-air-baltic-35f7648f-db47-41fd-a6aa-ce994621d1af" ["sale_date_begin"]=> string(10) "1477526400" ["sale_date_end"]=> string(10) "1477872000" ["flight_date_begin"]=> string(10) "1477958400" ["flight_date_end"]=> string(10) "1482105600" ["link"]=> string(163) "https://hydra.aviasales.ru/adaptors/special_offer?iata=BT&utm_source=www&locale=ru&url=https%3A%2F%2Fwww.airbaltic.com%2Fru-ZZ%2Fcampaign%2Fbaltic-incoming-sale%3F" } ["description"]=> object(SimpleXMLElement)#4 (0) { } ["conditions"]=> string(428) " Цены на авиабилеты указаны с учетом топливных, аэропортовых и государственных сборов. Количество авиабилетов на каждом рейсе по данному тарифу ограничено. Полные условия продажи авиабилетов на сайте авиакомпании. " ["route"]=> array(4) { [0]=> object(SimpleXMLElement)#5 (1) { ["@attributes"]=> array(7) { ["from_iata"]=> string(3) "LED" ["to_iata"]=> string(3) "TLL" ["from_name"]=> string(29) "Санкт-Петербург" ["to_name"]=> string(12) "Таллин" ["class"]=> string(12) "эконом" ["oneway_price"]=> string(11) "от 75 €" ["roundtrip_price"]=> string(0) "" } } [1]=> object(SimpleXMLElement)#6 (1) { ["@attributes"]=> array(7) { ["from_iata"]=> string(3) "LED" ["to_iata"]=> string(3) "PLQ" ["from_name"]=> string(29) "Санкт-Петербург" ["to_name"]=> string(16) "Клайпеда" ["class"]=> string(12) "эконом" ["oneway_price"]=> string(11) "от 75 €" ["roundtrip_price"]=> string(0) "" } } [2]=> object(SimpleXMLElement)#7 (1) { ["@attributes"]=> array(7) { ["from_iata"]=> string(3) "LED" ["to_iata"]=> string(3) "VNO" ["from_name"]=> string(29) "Санкт-Петербург" ["to_name"]=> string(14) "Вильнюс" ["class"]=> string(12) "эконом" ["oneway_price"]=> string(11) "от 75 €" ["roundtrip_price"]=> string(0) "" } } [3]=> object(SimpleXMLElement)#8 (1) { ["@attributes"]=> array(7) { ["from_iata"]=> string(3) "LED" ["to_iata"]=> string(3) "RIX" ["from_name"]=> string(29) "Санкт-Петербург" ["to_name"]=> string(8) "Рига" ["class"]=> string(12) "эконом" ["oneway_price"]=> string(11) "от 89 €" ["roundtrip_price"]=> string(0) "" } } } } 
  • add var_dump output for decoded structure to question - Anton Shchyrov
  • Your key is called not from_name and @from_name . - Arnial
  • @Arnial is so because it is actually converted not from json, but from xml. In json, I converted it manually to make it easier for the reader to read it. @ does not matter, because when converted to an array, it is deleted in the keys. - JamesJGoodwin
  • @AntonShchyrov added. - JamesJGoodwin
  • Just in case, how do you decode JSON, in an associative array ( $assoc = true ) or not? - Surfin Bird

2 answers 2

Well, sort of like a data object:

 $data = $offer->route[0]->attributes(); echo $data['from_name']; 

    Judging by the JSON format, all this is the first (and only) element of the array, so try to access it like this:

     <?php echo $offer[0]['route'][0]['from_name'];?> 
    • Nah , doesn't work - JamesJGoodwin
    • print_r($offer); what does he say? And to look at a piece of code from the moment of accessing api until data reaches $ offer - Samuel Loog