0

I have a PHP array I want to pass to my jquery (replace the test array with my php array).

Array
(
    [12] => Some Text
    [6] => Another text
    [11] => one more text
)

Jquery:

$('#SearchUser').typeahead({
                source: [
                    { ID: 1, Name: 'Toronto' },
                    { ID: 2, Name: 'Montreal' },
                    { ID: 3, Name: 'New York' },
                    { ID: 4, Name: 'Buffalo' },
                    { ID: 5, Name: 'Boston' },
                    { ID: 6, Name: 'Columbus' },
                    { ID: 7, Name: 'Dallas' },
                    { ID: 8, Name: 'Vancouver' },
                    { ID: 9, Name: 'Seattle' },
                    { ID: 10, Name: 'Los Angeles' }
                ],
                display: 'Name',
                val: 'ID',
                itemSelected: updateID
            });

So how can I set the var for "source" to my php array?

Suggestions?

As always,, you are awesome!

-Tom

1
  • Can you link to the typeahead plugin you are using? Commented Aug 7, 2012 at 22:33

4 Answers 4

1

How I'd done it in one of my projects was,

$.get("<?= $baseUrl ?>search/data", function (data) {
    var dataObject = JSON.parse(data);
    $('#q').typeahead({
        'source': dataObject,
        // 'items': 5
    });
});

and my search controller's data method is:

echo (json_encode($arr)); // $arr is ["a","b","c"] etc. but you can have an associated array too.

Basically, the point of showing my code is to say use json

Sign up to request clarification or add additional context in comments.

Comments

1

You can use json_encode function of php to conver a php array to json. Then you can use the json as javascript array.

http://www.php.net/manual/en/function.json-encode.php

I don't know how you get data from php code to javascript code, but if you print data as json, you can take it via an ajax call.

Comments

0

Use PHP's json_encode() function to create a string that is a valid JSON, and then read that with jQuery's $.getJSON().

Something like this:

get_array.php

$someArray = /* whatever */;
echo json_encode($someArray);

read_array.js

$.getJSON('get_array.php', function(data) {
    // data now contains your array
});

Alternatively, you could do something like this:

<script type="text/javascript">
    var someArray = <?php echo json_encode($someArray); ?>
</script>

If you don't want the separate files and don't mind using a global variable for it.

Comments

-1

You can do the following:

<script type="text/javascript">
    var jsArray = <?php echo json_encode($phpArray); ?>
</script>

but it is a bad practice to mix language X with language Y.

Well, take a look at my answer to this post in which you can do the following in a better way:

Is echoing Javascript code condtionally based on server-side logic considered harmful?

Hope this helps :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.