Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Suppose, I have this array in 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"
        )
    ),

);

How can I convert that to javascript with every keys same? I want the same array in javascript

share|improve this question

4 Answers 4

up vote 3 down vote accepted
<script>var cities = <?php echo json_encode($cities); ?>;</script>

then to access the data

<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>
share|improve this answer

You can use json_encode to convert the array to JSON:

echo json_encode($cities);
exit;
share|improve this answer
    
Please do that and show the code to do. Please show me how to access the $cities['Caracas']['air'][0] using javascript and json –  Prakash Aug 7 '12 at 5:27

Try json_encode(). JSON is the first, best, and only answer for php<->javascript data transfers.

share|improve this answer
    
How is the values accessible? –  Prakash Aug 7 '12 at 5:27
    
exactly the same as any other javascript data structure. arr['Caracas']['air'] –  Marc B Aug 7 '12 at 5:28
    
Please do that and show the code to do. Please show me how to access the $cities['Caracas']['air'][0] using javascript and json –  Prakash Aug 7 '12 at 5:28
    
That's core/basic javascript, and work exactly the same as in PHP. You know how to do it in PHP, so you know how to do it in JS. –  Marc B Aug 7 '12 at 5:29

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
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.