search
HomeBackend DevelopmentPHP ProblemHow to convert php array to json object

How to convert php array to json object

Aug 08, 2020 am 09:14 AM
json objectphp array

Methods for converting PHP arrays to json objects: 1. Convert ordinary PHP arrays to json arrays through the "json_encode($array);" method; 2. Convert ordinary PHP arrays to json arrays through the "urldecode(json_encode($arr));" Convert PHP array containing Chinese characters to json array.

How to convert php array to json object

Recommended: "PHP Video Tutorial"

Conversion between php arrays and json arrays

$array = array( //普通数组
              "name" => "Tom",
              "age" => 22
         );
echo "第一组:普通PHP数组转json数组——".json_encode($array); //json数组
echo "<br />";
//*注:urlencode()函数原理就是首先把中文字符转换为十六进制,然后在每个字符前面加一个标识符%。
//urldecode()函数是用于解码已编码的URL字符串,其原理就是把十六进制字符串转换为中文字符
$arr = array(  //中文数组
       &#39;0&#39; => urlencode(&#39;信息工程系&#39;),
       &#39;1&#39; => urlencode(&#39;机械工程系&#39;)
       );
echo "第二组:PHP包含中文的数组转json数组——".urldecode(json_encode($arr));
echo "<br />";
//*注:json数组的键与值若有中文时,中文只能用双引号“”,不能使用单引号,外面使用花括弧,并用单引号括起来
echo "第三组:json数组转PHP数组——";
$json =&#39;{"a":"哈哈","b":"我","c":"数","d":"一","e":3,"f":2,"g":1,"h":"别生气了"}&#39;;    
var_dump(json_decode($json,true));

The above is the detailed content of How to convert php array to json object. 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),