In front-end development, there are often situations where it is necessary to convert arrays in PHP into JavaScript object arrays. Of course, this is not difficult and only requires simple operations. This article will introduce a method to directly convert a PHP array into a JavaScript object array using the json_encode()
function.
Before converting PHP array to JavaScript object array, you need to convert PHP array to JSON string first. Use the PHP built-in function json_encode()
to convert a PHP array into a JSON string. Here is an example:
$data = array( array('name' => 'Amy', 'age' => 22), array('name' => 'Bob', 'age' => 25), array('name' => 'Cathy', 'age' => 28) ); $json_string = json_encode($data); echo $json_string;
The above code converts the $data array into a JSON string and outputs it using echo
. The output result is:
[{"name":"Amy","age":22},{"name":"Bob","age":25},{"name":"Cathy","age":28}]
After getting the JSON string, you only need to use the JSON.parse()
function to convert it Just convert it into a JavaScript object. The following is an example:
var data = JSON.parse('<?php echo $json_string; ?>'); console.log(data);
The above code outputs the value of $json_string
to the JSON.parse()
function, converts it into a JavaScript object, and uses console.log()
Output. The output result is:
[ { name: "Amy", age: 22 }, { name: "Bob", age: 25 }, { name: "Cathy", age: 28 } ]
The above method can simply convert a PHP array into a JavaScript object array. If you need to use it as a function or variable in JavaScript, you can directly concatenate the above code into a string. For example:
var data = JSON.parse('<?php echo json_encode($data); ?>');
In addition to the methods introduced above, you can also use PHP’s built-in function array_map()
The function will convert each element in the array to elements into JavaScript objects. The following is an example of using the array_map()
function:
$data = array( array('name' => 'Amy', 'age' => 22), array('name' => 'Bob', 'age' => 25), array('name' => 'Cathy', 'age' => 28) ); $js_array = array_map(function($item) { return "{'name': '{$item['name']}', 'age': {$item['age']}}"; }, $data); echo "[" . implode(",", $js_array) . "]";
The above code uses the array_map()
function to convert each element in the $data
array Convert elements into JavaScript objects and store them in the $js_array
array. Finally, the $js_array
array is converted into JSON array output. The output result is:
[{'name': 'Amy', 'age': 22},{'name': 'Bob', 'age': 25},{'name': 'Cathy', 'age': 28}]
This article introduces two methods of converting PHP arrays into JavaScript object arrays. One of them is to use the json_encode()
function directly Convert a PHP array to a JSON string and then use the JSON.parse()
function to convert it to a JavaScript object array. The other is to use the array_map()
function to convert each element of the PHP array into a JavaScript object. Both methods can convert PHP arrays into JavaScript object arrays, and readers can choose which method according to their own needs.
The above is the detailed content of How to convert php array to js object array. For more information, please follow other related articles on the PHP Chinese website!