1

I have a php array

array(6) { 
   ["merchant_id"]=> string(6) "ajeesh" 
   ["passkey"]=> string(4) "1234" 
   ["amt"]=> string(5) "10.00" 
   ["email"]=> string(16) "ajeesh@gmail.com" 
   ["mobileNo"]=> string(10) "9874563210" 
   ["orderID"]=> string(6) "123456" 
}

which I got as a result of var_dump($_POST).

How can I copy all this value to a javascript array variable?How can it be possible? Suppose if the javascript array I made is

var thisSession = new Array();

TRY

I have tried this in the javascript

<script>
window.onload = function getApp(){
    var thisSession = JSON.parse('<?php echo json_encode($_POST); ?>');
    alert (thisSession);
}
</script>

and this in the php

json_encode($_POST);

but the javascript is alerting "Object object".Im not gettign the array!why?

Ahmed Siouani
  • 13,701
  • 12
  • 61
  • 72
Ajeesh
  • 5,650
  • 8
  • 30
  • 52
  • 2
    use `json_encode()` function http://www.php.net/manual/en/function.json-encode.php – ChoiZ Dec 27 '13 at 15:22
  • Can you be more specefic.Ill check the documentation. – Ajeesh Dec 27 '13 at 15:23
  • json_encode() returns the JSON representation of your array You have to use the return in javascript in a second time – ChoiZ Dec 27 '13 at 15:25
  • maybe this link can help? http://stackoverflow.com/questions/5618925/convert-php-array-to-javascript – Goikiu Dec 27 '13 at 15:26
  • So as you gathered `json_encode` + _don't use `new Array();` in JS_ Use the literal notation `var anArray = [];` It's shorter and can't result in unexpected behaviour in the same way the constructor can – Elias Van Ootegem Dec 27 '13 at 15:30

4 Answers4

2

You can use JSON, encode the PHP variable, then parse it in JS:

var thisSession = JSON.parse('<?php echo json_encode($_POST); ?>');
Shomz
  • 37,421
  • 4
  • 57
  • 85
  • Oh thanks. let me try that and will let know about it – Ajeesh Dec 27 '13 at 15:24
  • No problem, I wrote this from the top of my head, so it might need tweaking, but the concept has to work. For example: `JSON.parse('{"foo":"bar","baz":"long"}')` – Shomz Dec 27 '13 at 15:25
  • yeah sure,Ill let you know – Ajeesh Dec 27 '13 at 15:27
  • It's because JS doesn't have associative arrays like PHP does. It uses objects instead. Try replacing alert with console.log and see the whole object in the console: `console.log(thisSession);` – Shomz Dec 27 '13 at 15:44
  • One more thing I wish asking you,fromthe parsed array in the javascript,is it possible for search for somthing in it?or if I want to get each element in that array seperately what can I do?I will not get it by var a=thisSession[0]; to get the first element I guess? – Ajeesh Dec 27 '13 at 15:49
  • No, you need to specify the keys, like: `thisSession.merchant_id` or `thisSession['merchant_id']`... Or, loop through all the properties like: `for (var i in thisSession) {alert('key: ' + i + ' value: ' + thisSession[i])};` – Shomz Dec 27 '13 at 15:52
1

using php's json_encode & javascript's JSON.parse

var thisSession=JSON.parse('<?php echo json_encode($phparray)?>');
cocco
  • 16,442
  • 7
  • 62
  • 77
1

EDIT
If you want to access merchant_id you simply do

alert(thisSession.merchand_id);

Here, you need to json_encode the php data to use in javascript

  $array = json_encode($_POST);

In your html

<script>
var data = JSON.parse("<?php echo $array; ?>"); // your new javascript object
</script>

Reference http://www.php.net/json_encode

-2

There is no comparable object (associative array) in JavaScript to that which you've shown us in your example. You'd either have to use two Arrays or lose the ordering (and ability to have multiple keys of the same name) by using an Object. This second option is what json_encode will result in.

Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • ordering doesnt matter.But I just want to have all those values which is in the php to a javascript array – Ajeesh Dec 27 '13 at 15:23