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) "[email protected]" 
   ["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?

5
  • 2
    use json_encode() function php.net/manual/en/function.json-encode.php Commented Dec 27, 2013 at 15:22
  • Can you be more specefic.Ill check the documentation. Commented Dec 27, 2013 at 15:23
  • json_encode() returns the JSON representation of your array You have to use the return in javascript in a second time Commented Dec 27, 2013 at 15:25
  • maybe this link can help? stackoverflow.com/questions/5618925/… Commented Dec 27, 2013 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 Commented Dec 27, 2013 at 15:30

4 Answers 4

2

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

var thisSession = JSON.parse('<?php echo json_encode($_POST); ?>');
Sign up to request clarification or add additional context in comments.

6 Comments

Oh thanks. let me try that and will let know about it
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"}')
yeah sure,Ill let you know
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);
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?
|
1

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

var thisSession=JSON.parse('<?php echo json_encode($phparray)?>');

Comments

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

4 Comments

add quotes before and after the php echo.& JSON.parse.else it won't work.
@cocco that would actually return just a string and would require additinal parsing
Thanks I wish I could upvote.Once Im capable I will.Your answer helped!
Welcome, glad that it's helpful
-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.

1 Comment

ordering doesnt matter.But I just want to have all those values which is in the php to a javascript array

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.