Intereting Posts
Автоматическое обновление этого столбца после 24:00 (0.00 AM) с помощью PHP? Проверка наличия элементов одного массива в другом массиве в PHP cUrl Возможно преобразование в Javascript? Как правильно использовать сеансы для динамических страниц Формат mm / dd / yyyy в эпоху с PHP Каковы параметры «$ ns» и «$ is_prefix»? multi textfield с автозаполнением, но почему только первый Проверьте причину медленного времени загрузки между разными серверами Преобразование фракций в html-объекты Prestashop 1.6 – добавьте настраиваемое поле в категорию Получить высоту и ширину изображения PHP Как использовать активную загрузку здесь, я имею в виду только одну переменную вместо двух. Поэтому мне не нужно запускать два отдельных запроса Приложение Laravel не работает после загрузки на сервер Создание таблицы mysql с переменной PHP Как изменить схему URL-адреса с помощью preg_match?

Массив PHP для массива javascript

Предположим, у меня есть этот массив в php

$cities=array( "Caracas"=>array( "air"=>array( "4","3-5 Working Days","Saturday" ), "sea"=>array( "18","3-5 Days","Wednesday" ) ), "Los Teques"=>array( "air"=>array( "4","3-5 Working Days","Saturday" ), "sea"=>array( "18","3-5 Days","Wednesday" ) ), "La Victoria"=>array( "air"=>array( "4","3-5 Working Days","Saturday" ), "sea"=>array( "18","3-5 Days","Wednesday" ) ), "Maracay"=>array( "air"=>array( "4","3-5 Working Days","Saturday" ), "sea"=>array( "18","3-5 Days","Wednesday" ) ), "Tejeria"=>array( "air"=>array( "4","3-5 Working Days","Saturday" ), "sea"=>array( "18","3-5 Days","Wednesday" ) ), "La Encrucijada"=>array( "air"=>array( "4","3-5 Working Days","Saturday" ), "sea"=>array( "18","3-5 Days","Wednesday" ) ), "Cagua"=>array( "air"=>array( "4","3-5 Working Days","Saturday" ), "sea"=>array( "18","3-5 Days","Wednesday" ) ), "Turmero"=>array( "air"=>array( "4","3-5 Working Days","Saturday" ), "sea"=>array( "18","3-5 Days","Wednesday" ) ), "San Mateo"=>array( "air"=>array( "4","3-5 Working Days","Saturday" ), "sea"=>array( "18","3-5 Days","Wednesday" ) ), "Margarita"=>array( "air"=>array( "4","3-5 Working Days","Saturday" ), "sea"=>array( "18","3-5 Days","Wednesday" ) ), "Valencia"=>array( "air"=>array( "4","3-5 Working Days","Saturday" ), "sea"=>array( "18","3-5 Days","Wednesday" ) ), // another section "Puerto la Cruz"=>array( "air"=>array( "4.50","5-10 Working Days","Saturday" ), "sea"=>array( "19","4-6 Days","Wednesday" ) ), "Lecheria"=>array( "air"=>array( "4.50","5-10 Working Days","Saturday" ), "sea"=>array( "19","4-6 Days","Wednesday" ) ), "Bacelona"=>array( "air"=>array( "4.50","5-10 Working Days","Saturday" ), "sea"=>array( "19","4-6 Days","Wednesday" ) ), "Guanta"=>array( "air"=>array( "4.50","5-10 Working Days","Saturday" ), "sea"=>array( "19","4-6 Days","Wednesday" ) ), "Puerto Piritu"=>array( "air"=>array( "4.50","5-10 Working Days","Saturday" ), "sea"=>array( "19","4-6 Days","Wednesday" ) ), // another section "Cumana"=>array( "air"=>array( "5.50","5-10 Working Days","Saturday" ), "sea"=>array( "19.50","4-6 Days","Wednesday" ) ), "Anaco"=>array( "air"=>array( "5.50","5-10 Working Days","Saturday" ), "sea"=>array( "19.50","4-6 Days","Wednesday" ) ), "Cantaura"=>array( "air"=>array( "5.50","5-10 Working Days","Saturday" ), "sea"=>array( "19.50","4-6 Days","Wednesday" ) ), "El Tigre"=>array( "air"=>array( "5.50","5-10 Working Days","Saturday" ), "sea"=>array( "19.50","4-6 Days","Wednesday" ) ), // another section "Maturin"=>array( "air"=>array( "6","5-10 Working Days","Saturday" ), "sea"=>array( "20","4-6 Days","Wednesday" ) ), "Punta de Mata"=>array( "air"=>array( "6","5-10 Working Days","Saturday" ), "sea"=>array( "20","4-6 Days","Wednesday" ) ), "Ciudad Bolivar"=>array( "air"=>array( "6","5-10 Working Days","Saturday" ), "sea"=>array( "20","4-6 Days","Wednesday" ) ), "Puerto Ordaz"=>array( "air"=>array( "6","5-10 Working Days","Saturday" ), "sea"=>array( "20","4-6 Days","Wednesday" ) ), "Carupano"=>array( "air"=>array( "6","5-10 Working Days","Saturday" ), "sea"=>array( "20","4-6 Days","Wednesday" ) ), ); 

Как я могу преобразовать это в javascript с каждым ключом? Мне нужен тот же массив в javascript

 <script>var cities = <?php echo json_encode($cities); ?>;</script> 

затем для доступа к данным

 <script> cities['San Mateo']['Air'][0]; // 4 cities['San Mateo']['Air'][1]; // 3-5 Working Days // etc // looping for( var key in cities) { // key = city name } </script> 

Попробуйте json_encode() . JSON – это первый, лучший и единственный ответ для передачи данных в формате php <-> javascript.

Вы можете использовать json_encode для преобразования массива в JSON:

 echo json_encode($cities); exit; 

PHP

 echo json_encode($cities); 

JQuery

 $.getJSON("php_file.php",function(data) { // "data" is the echoed encoded array from the php file. // "array"."city name"."'air' or 'sea'"."number value that is in the PHP file" data.Caracas.air.4 // this is how you access the data inside the array });