Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have a php multidimensional array that I am sending to jquery but I need to automatically create the array.

All the examples show setting up a manual array like so:

var theme_name    = current_theme_meta.theme_name,
    theme_version = current_theme_meta.version,
    data0A        = theme_metadata[0].dataA,
    data0B        = theme_metadata[0].dataB,
    data1A        = theme_metadata[1].dataA,
    data1B        = theme_metadata[1].dataB;

current_theme_meta and theme_metadata are keys in php array I built and I push to jQuery via wp_localize_script() (a wordpress function.)

theme_name, version, dataA, and dataB are key values inside the array.

My array looks like this:

[current_theme_meta] => Array
    (
        [theme_name] => A Cool Theme
        [version] => 2.1.1
    )

[theme_meta] => Array
    (
        [0] => Array
            (
                [dataA] => foo
                [dataB] => bar
            )

        [1] => Array
            (
                [dataA] => this
                [dataB] => that
            )
    )

How do I create the array in jquery? I am confused between each and loop, etc.

share|improve this question

json_encode in your PHP, then parse the json in your jQuery:

$json = json_encode($your_array); 

Then access it, and parse it in your Javascript:

var yourArray = JSON.parse('<?php echo $json ;?>'); 

JSON similar to XML is great for exchanging data between languages, as most languages have built in functions for dealing with it - as you can see in this example.

share|improve this answer

A quick and easy way to do it would be to encode your PHP array as JSON using json_encode, and then pass it to your JavaScript.

An example would be:

<?php
    //Create & populate your PHP array
    $my_php_array = array(
        'foo'  => 'bar',
        'test' => array(
                'abc' => '123'
            )
    );
?>

<script type="text/javascript">
    var my_javascript_object = JSON.parse("<?php echo json_encode($my_php_array); ?>");
</script>

After that, you can access your JavaScript object as such:

<script type="text/javascript">
    alert(my_javascript_object.foo);
    alert(my_javascript_object.test.abc);
</script>
share|improve this answer
    
That does sound like the easiest way...so I basically do not need to setup the array on the jquery side as the key => value will just be passed and I can call the key/value pair. Thanks, I will play around with how to pass the array as json from inside wordpress. – Jason Aug 5 '13 at 19:00
    
Yes, that's exactly it. You'll do all your array building/manipulation in PHP, and pass the completed array as a JSON string, which is then parsed by JavaScript into an object. – Alessandro Bassi Aug 5 '13 at 19:12

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.