Home  >  Article  >  Backend Development  >  How to convert array to json data in php, convert double quotes to single quotes

How to convert array to json data in php, convert double quotes to single quotes

青灯夜游
青灯夜游Original
2022-08-18 18:22:542227browse

In PHP, you can use the json_encode() function to convert the array into json data, the syntax is "json_encode($arr)"; you can use the str_replace() or preg_replace() function to convert double quotes into single Quotes, just find the double quotes in the string and replace them with single quotes, the syntax is "str_replace('"',"'",string)" or "preg_replace('/\"/',"' ", string)".

How to convert array to json data in php, convert double quotes to single quotes

The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer

php will convert the array For json data

In php, you can use the json_encode() function to convert the array into json data.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr = array (&#39;a&#39;=>1,&#39;b&#39;=>2,&#39;c&#39;=>3,&#39;d&#39;=>4,&#39;e&#39;=>5);
echo "原数组:";
var_dump($arr);
echo "转为json后:<br><br>";
echo json_encode($arr);
?>

How to convert array to json data in php, convert double quotes to single quotes

Description: json_encode() function

json_encode() function can JSON encode variables

Syntax:

json_encode ( $value [, $options = 0 ] )

Returns a string, containing the representation of value in JSON form.

Note:

1. $value is the value to be encoded, and this function is only valid for UTF8 encoded data;

2. options: consists of the following constants Binary masks: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT;

3. The second parameter is generally not needed;

4. JSON data is actually a string , you can use var_dump() to print it out to see the data type;

5. If the execution is successful, JSON data will be returned, otherwise FALSE will be returned.

Return value:

  • Returns a JSON encoded string on success or false on failure.

PHP Convert double quotes to single quotes

Method 1: Use str_replace() function

str_replace() function replaces some characters in a string (case sensitive).

Just use this function to find double quotes in a string and replace them with single quotes.

Note: The same type of quotation marks cannot be nested (single quotation marks cannot contain single quotation marks, and double quotation marks cannot contain double quotation marks). You can use "outer double and inner single" or "outer single and inner double" "Format

Example:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$param = &#39;{"id":"12", "name":"hi"}&#39;;
echo "原字符串:".$param."<br>";
$new = str_replace(&#39;"&#39;,"&#39;",$param);
echo "新字符串:".$new;
?>

How to convert array to json data in php, convert double quotes to single quotes

Method 2: Use the preg_replace() function with regular expressions to convert single quotes The preg_replace function performs a regular expression search and replace for double quotes

.

Example:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$param = &#39;{"id":"12", "name":"hi","ang":"23"}&#39;;
echo "原字符串:".$param."<br>";
$new = preg_replace(&#39;/\"/&#39;, "&#39;", $param);
echo "新字符串:".$new;
?>

How to convert array to json data in php, convert double quotes to single quotes

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert array to json data in php, convert double quotes to single quotes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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