search
HomeBackend DevelopmentPHP ProblemHow to convert php array to json string

How to convert php array to json string

Aug 31, 2020 am 09:25 AM
jsonphp array

How to convert php array to json string: first create a php sample file; then define a set of array data; finally convert the array into a json string through the "json_encode($color)" method.

How to convert php array to json string

Recommended: "PHP Video Tutorial"

Convert array/object in PHP to JSON string

The code is as follows:

<?php
 
//php语言生成json字符串
//json_encode(数组/对象);
 
//① 索引数组-->JavaScript数组
$color = array(&#39;gold&#39;,&#39;yellow&#39;,&#39;blue&#39;);
echo json_encode($color)."<br />";// ["gold","yellow","blue"]
 
//② 关联数组-->json字符串
$city = array(&#39;shandong&#39;=>&#39;jinan&#39;,&#39;henan&#39;=>&#39;zhengzhou&#39;,&#39;hebei&#39;=>&#39;shijiazhuang&#39;);
echo json_encode($city)."<br />";//{"shandong":"jinan","henan":"zhengzhou","hebei":"shijiazhuang"}
 
//③ 索引关联数组-->json字符串
$city2 = array(&#39;shandong&#39;=>&#39;jinan&#39;,&#39;henan&#39;=>&#39;zhengzhou&#39;,&#39;shenyang&#39;,&#39;hebei&#39;=>&#39;shijiazhuang&#39;);
echo json_encode($city2)."<br />";//{"shandong":"jinan","henan":"zhengzhou","0":"shenyang","hebei":"shijiazhuang"}
 
//④ 对象生成json信息-->json字符串
//   (只会序列化"成员属性",不会序列化"成员方法")
class Person{
    //类内部的成员属性需要有修饰符(var/public/protected/private),其是固定语法
    public $color = "yellow";
    var $height = 170;
    function run(){
        echo "is running";
    }
}
$tom = new Person();
echo json_encode($tom);  //{"color":"yellow","height":170}

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

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

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),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor