2
var stds = [];
<?php
  foreach ($this->students as $key => $value){
?>
    var smval = "<?php echo $value?>";
    stds.push( smval );
<?php
}
?>
alert("stds"+stds);

alert("stds"+stds);//print abc,bcd,efg so on...

The above code works fine for pushing Student Names. Now I want to push student CODES also which is there in value $key. How can i create an associative array to do that

1
  • 1
    This seems to be badly vulnerable to XSS. Commented Feb 29, 2012 at 20:05

4 Answers 4

3

The cleanest and safest way to do this is to convert the PHP object into a JavaScript object literal with json_encode:

var students = <?= json_encode($this->students) ?>;

// sample usage
alert (students["Tony"]);   // Tony's student code
alert (students["Geezer"]); // Geezer's student code
alert (students["Vinnie"]); // etc.
Sign up to request clarification or add additional context in comments.

1 Comment

Two bad things about this example: you don't need to put quotes around what json_encode returns (or call jQuery.parseJSON) and you don't need to escape quotes, json_encode always returns something that is valid JavaScript, therefore you can just use var students = <?= json_encode($this->students) ?>;
1
var stds = [];
<?php
  foreach ($this->students as $key => $value){
?>
    stds.push( { 'name': '<?php echo $value; ?>', 'code': '<?php echo $key; ?>' } );
<?php
  }
?>
alert("stds"+stds);

To be precise: There are no associative arrays in JavaScript. You can either use the code above, which gives you an array of objects containing your data or the code below, which gives you an object similar to the one you have in PHP.

var stds = {};
<?php
  foreach ($this->students as $key => $value){
?>
    stds[ '<?php echo $key; ?>' ] = '<?php echo $value; ?>';
<?php
  }
?>
alert("stds"+stds);

9 Comments

when i try to print the stds in the alert .It is printing object , object
it is an array of objects in the first code example. you would access each entry by stds[0].name, for example.
Incorrect. {"key1": "val1", "key2": "val2"} is an associative array (ie, a hash).
that would be an object. it does not have any of the array prototype's functions and properties like length or map.
Google "JavaScript associative array". Plenty of information on this out there. You're confusing the array datatype with general objects. All objects in JavaScript are also associative arrays. Even functions.
|
0
var stds = <?php echo json_encode( $this->students); ?>;

Not sure what keys are in your array

alert( stds[0][ key1 ] +'   ' + stds[0][ key2 ])

Comments

0

I think you're trying to do more of a

stds[<?php echo $key?>] = '<?php echo $value; ?>';

or you can try doing it within an object demonstrated here:

http://jsfiddle.net/u3Muk/

1 Comment

Don't do this, you need to escape quotes and newlines in your strings. Just call json_encode to do the heavy lifting for you. Converting PHP arrays and values to JS is simpler than most think.

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.