Jump to content

how to use jason_encode


new_member

Recommended Posts

Hi,
 
i have an array with five values. I want to encode the array using json_encode in format:
 
{"value1":"val","data":["name", "email"]}
 
where value 1 is a paremeter in php and data is an array. But if i use json_encode it gives me different output

here is my code: $arr['value1'] = 'val';$arr['data'] = $data //arrayjson_encode($arr); it gives me {"value1":"val1","data":{"name", "email"}} 

 
can any one tell men how to encode the array in above format??
 
thanks

Link to comment
https://forums.phpfreaks.com/topic/280588-how-to-use-jason_encode/
Share on other sites

Thanks for the reply. The solution you provided actually worked. But i found another problem with my array as i am passing the array values through ajax and getting using $_GET. I have two fields "name" and "email" and values are passed as

 

name: "abc"

email: "[email protected]"

 

which actually gives

 

$data['name'] = "abc"

$data['email'] = "[email protected]"

 

{data:{"name":"abc"}} etc

 

is there any way to pass the array in format without name and email label and pass the values only

 

like

 

data = array("abc", "[email protected]")

 

i actually dont want to get

Have you tried just setting your array like this:

$data = array();
$data[] = "abc";
$data[] = "[email protected]";

or pretty much exactly as you just wrote it:

 

$data = array("abc", "[email protected]");

However personally I would use the key-value pairs, as that way you know exactly what data you're grabbing in the javascript, instead of just an index number.

 

Denno

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.