Let’s suppose you are working on a project which requires you to convert your php array into javascript json array.

The easiest way to do this is with a PHP function call json_encode.

Example 1 - Simple PHP Array Into Javascript JSON Object

Assuming you have the following php array:

<?php

$arr = array(
	'name' => "mark", 
	'site' => 'codewithmark.com', 
	'post_name' => 'json encode javascript', 
	'post_content' => 'learn to how encode php array into javascript array', 
);

echo json_encode($arr);

?>

The above will be converted in javascript json object:

{
 'name' : "mark", 
 'site': 'codewithmark.com', 
 'post_name' : 'json encode javascript', 
 'post_content' : 'learn to how encode php array into javascript array',
}

Example 2 - PHP Multi Arrays Into Javascript JSON Arrays

Assuming you have the following php array:

<?php

$arr = array(
	array('name' => "mark", 'site' => 'codewithmark.com',),
	array('name' => "youtube", 'site' => 'youtube.com',),
	array('name' => "gmail", 'site' => 'gmail.com',),
);
echo json_encode($arr);

?>

The above will be converted in javascript json array:

[
	{'name' : "mark", 'site': 'codewithmark.com',},
	{'name' : "youtube", 'site': 'youtube.com',},
	{'name' : "gmail", 'site': 'gmail.com',},
]

Example 3 - Javascript Ajax Call 

Now that you have a basic understanding of how to encode php array into javascript json array, let's make a jquery ajax call to your php file to see if user exist in your mysql database.

Jquery ajax call file:

//data you want to send to your php file
var data_obj = {
	user_id: id.val(), //assuming this is your data value
	user_email: email.val(),//assuming this is your data value
}

$.post('your-php-file.php', data_obj, function(data) 
{
	//convert it into json array to be on the safe side
	var json_data = JSON.parse(data);

	//do your data processing below
});

Your php file (your-php-file.php):

<?php

$user_id 	= $_POST['user_id'];
$user_email = $_POST['user_email'];

//check to see if user is in your mysql database
$check_user	= $db->select("select * from tbl_users  where user_id='$user_id' and user_email='$user_email' ");

if( $check_user > 1)
{
	$Result =  array(
		'status' =>"error" , 
		'msg' => 'no user found',			
	);
	echo json_encode($Result );
	die();
}

$Result =  array(
	'status' =>"success" , 
	'msg' => 'found user',
	'user_data'=> $check_user, //user data array		 
);
echo json_encode($Result );
 
?>

$db->select() function is part of the "PHP Simple Database Class"

Example 4 - JSON Encode Data Directly Into Javascript

If you want to include your php json encoded data on page load, you can easily do that all in your php file and the out the result in javascript like this:

//this is all in one file
<?php

$arr = array(
	'name' => "mark", 
	'site' => 'codewithmark.com', 
	'post_name' => 'json encode javascript', 
	'post_content' => 'learn to how encode php array into javascript array', 
);

?>

<script type="text/javascript">

var d1 = <?php echo json_encode($book, JSON_PRETTY_PRINT) ?>;
/* 
var d1 = {
 'name' : "mark", 
 'site': 'codewithmark.com', 
 'post_name' : 'json encode javascript', 
 'post_content' : 'learn to how encode php array into javascript array',
}; 
*/

console.log(d1.site); //output >>> codewithmark.com

</script>






Name

Email

Website

Comment

Post Comment