Home > Backend Development > PHP Problem > How to convert php array to js object array

How to convert php array to js object array

PHPz
Release: 2023-04-23 19:35:39
Original
710 people have browsed it

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.

Convert PHP array to JSON string

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;
Copy after login

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}]
Copy after login

Convert JSON string into JavaScript object array

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);
Copy after login

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 }
]
Copy after login

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); ?>');
Copy after login

Use PHP’s array_map() function

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) . "]";
Copy after login

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}]
Copy after login

Summary

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template