Asked  2 Years ago    Answers:  5   Viewed   59 times

Please provide help on how to assign php array values to javascript array

----------- Using below php code I am storing data in php array -----------

$str_query="SELECT title,description FROM tablename ORDER BY title";

$schd_arr = array();
$schd_arr[$int_count] = $rs_sch->Fields("title").": ".$rs_sch->Fields("description");

----- Using below javascript code I am trying to store php array data into javascript array -----

Please let me know what and how to write variable at below 2 mentioned locations so my code can work.

<script type="text/javascript" language="javascript">
var pausecontent=new Array()
for (sch_cnt=0; sch_cnt<*Here I want to assign value of $int_count php variable*; sch_cnt++)
pausecontent[sch_cnt]=<?php *Here I want to assign php array and counter values (something like this - $schd_arr[sch_cnt];)* ?>;

Thank you in advance, KRA



You can't loop like that, you need to loop the PHP array and push into javascript array:

<script type="text/javascript" language="javascript">
    var pausecontent = new Array();
    <?php foreach($schd_arr as $key => $val){ ?>
        pausecontent.push('<?php echo $val; ?>');
    <?php } ?>
Saturday, August 27, 2022

No, it doesn't have one. For this reason most popular libraries come with one in their utility packages. Check out jQuery's inArray and Prototype's Array.indexOf for examples.

jQuery's implementation of it is as simple as you might expect:

function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    return false;

If you are dealing with a sane amount of array elements the above will do the trick nicely.

EDIT: Whoops. I didn't even notice you wanted to see if an array was inside another. According to the PHP documentation this is the expected behavior of PHP's in_array:

$a = array(array('p', 'h'), array('p', 'r'), 'o');

if (in_array(array('p', 'h'), $a)) {
    echo "'ph' was foundn";

if (in_array(array('f', 'i'), $a)) {
    echo "'fi' was foundn";

if (in_array('o', $a)) {
    echo "'o' was foundn";

// Output:
//  'ph' was found
//  'o' was found

The code posted by Chris and Alex does not follow this behavior. Alex's is the official version of Prototype's indexOf, and Chris's is more like PHP's array_intersect. This does what you want:

function arrayCompare(a1, a2) {
    if (a1.length != a2.length) return false;
    var length = a2.length;
    for (var i = 0; i < length; i++) {
        if (a1[i] !== a2[i]) return false;
    return true;

function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(typeof haystack[i] == 'object') {
            if(arrayCompare(haystack[i], needle)) return true;
        } else {
            if(haystack[i] == needle) return true;
    return false;

And this my test of the above on it:

var a = [['p','h'],['p','r'],'o'];
if(inArray(['p','h'], a)) {
    alert('ph was found');
if(inArray(['f','i'], a)) {
    alert('fi was found');
if(inArray('o', a)) {
    alert('o was found');
// Results:
//   alerts 'ph' was found
//   alerts 'o' was found

Note that I intentionally did not extend the Array prototype as it is generally a bad idea to do so.

Thursday, November 24, 2022

The line:

var dataToSend = {'name' : val, 'ans[]' : <?php echo $answers; ?> } ;

will print:

var dataToSend = {'name' : val, 'ans[]' : Array } ;

which creates a javascript syntax semantic error (ans = empty string will be posted). Change to:

var dataToSend = {'name' : val, 'ans[]' : <?php echo json_encode($answers); ?> } ;

which prints:

var dataToSend = {'name' : val, 'ans[]' : [13,5,6,11] } ;
Tuesday, November 8, 2022

Lots of little issues going on here ... the biggest problem, though, is that you can't use a simplexml object node as an index in an array. It has to be manually cast as a string or integer. You'd also be better served tweaking your xpath expression a bit, and your loop shouldn't be on $person, which is a variable that doesn't exist, but instead on $category. Try this as an alternative:

$array = array();
$category = $xml->xpath('//person');
while(list( , $p) = each($category)) {
        $array[(string)$p['id']] = (string)$p['name'];

Also note that, if your XML is not valid XML, then it does matter ... simplexml libraries will never function properly on invalid XML (the XML in your example has some improper nesting).

Sunday, October 23, 2022

You can switch the version here:

1. Press CTRL+ALT+S

2 Search for JavaScript & click the select field. and then select ECMAScript 6

Wednesday, August 3, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages