A
A
alexbruni2014-08-13 06:13:03
PHP
alexbruni, 2014-08-13 06:13:03

How to pass data from PHP array to variables?

Hello! Never encountered an array in PHP...
Here it is:

var_export($SxGeo->getCityFull($ip)); // Вся информация о городе

Displays information about the city:
array ( 'city' => array ( 'id' => 520555, 'lat' => 56.32867, 'lon' => 44.00205, 'name_ru' => 'Нижний Новгород', 'name_en' => 'Nizhniy Novgorod', 'okato' => '22401', ), 'region' => array ( 'id' => 559838, 'lat' => 56, 'lon' => 45, 'name_ru' => 'Нижегородская область', 'name_en' => 'Nizhegorodskaya Oblast\'', 'iso' => 'RU-NIZ', 'timezone' => 'Europe/Moscow', 'okato' => '22', ), 'country' => array ( 'id' => 185, 'iso' => 'RU', 'continent' => 'EU', 'lat' => 60, 'lon' => 100, 'name_ru' => 'Россия', 'name_en' => 'Russia', 'timezone' => 'Europe/Moscow', ), )

How to separate information into variables? For example, so that, for example, the $name_ru variable would display "Nizhny Novgorod"?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel Belousov, 2014-08-13
@alexbruni

The purpose of translating an array into variables is not entirely clear, because you can use it like this:

$arr = $SxGeo->getCityFull($ip);
echo $arr['city']['name_ru'];

1
1001001 111, 2014-08-13
@IgorO2

Use the extract()
function Example from php.net/manual/ru/function.extract.php

<?php

/* Предположим, что $var_array - это массив, полученный в результате
   wddx_deserialize */

$size = "large";
$var_array = array("color" => "blue",
                   "size"  => "medium",
                   "shape" => "sphere");
extract($var_array, EXTR_PREFIX_SAME, "wddx");

echo "$color, $size, $shape, $wddx_size\n";

?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question