php How to convert json string to array: First use the "json_decode" function to encode the string in JSON format; then print "$students" through the "var_dump($students);" method.
Recommended: "PHP Video Tutorial"
php json string to array or object
The method found on the Internet is to use get_object_vars to convert the class type into an array and then use foreach to traverse it
$array = get_object_vars($test); $json= '[{"id":"1","name":"\u5f20\u96ea\u6885","age":"27","subject":"\u8ba1\u7b97\u673a\u79d1\u5b66\u4e0e\u6280\u672f"},{"id":"2","name":"\u5f20\u6c9b\u9716","age":"21","subject":"\u8f6f\u4ef6\u5de5\u7a0b"}]';
First, use json_decode to encode the string in JSON format,
$students = json_decode($json);
Use $students directly in the PHP file:
for($i=0;$i<count($students);$i++){ echo "姓名:".$students[$i]['name']."年龄:".$students[$i]['age']."专业:".$students[$i]['subject']."<br/>"; }
, the error will be reported as follows:
Fatal error : Cannot use objectof type stdClass as array in D:\wamp\www\test.php on line 18
At this time, print $students:
var_dump($students);
will output:
array(2) { [0]=> object(stdClass)#2 (4) { ["id"]=> string(1)"1" ["name"]=> string(9)"张雪梅" ["age"]=> string(2)"27" object(stdClass)#3 (4) { 这个就说明转换的json字符串转为对象而非数组,请看下面的红色背景字
["subject"]=>string(24) "计算机科学与技术" } [1]=> ["id"]=> string(1)"2" ["name"]=> string(9)"张沛霖" ["age"]=> string(2)"21" ["subject"]=> string(12) "软件工程" } }
It can be seen that the returned result is object instead of array. Should be accessed in object form:
foreach($students as $obj){ echo "姓名:".$obj->name."年龄:".$obj->age."专业:".$obj->subject."<br/>"; }
The output result is:
Name: Zhang Xuemei Age: 27 Major: Computer Science and Technology
Name: Zhang Peilin Age: 21 Major: Software Engineering
mixedjson_decode ( string$json [, bool$assoc ] )
Description: Accept a JSON format string and convert it Convert to PHP variable.
json_decode can receive two parameters:
json: the string in jsonstring format to be decoded.
assoc: When this parameter is TRUE, an array will be returned instead of an object.
$students = json_decode($json,true);
Print $students at this time:
var_dump($students);
Output:
array(2) { [0]=> array(4) { ["id"]=> string(1)"1" ["name"]=> string(9)"张雪梅" ["age"]=> string(2)"27" ["subject"]=>string(24) "计算机科学与技术" } [1]=> array(4) { ["id"]=> string(1)"2" ["name"]=> string(9)"张沛霖" ["age"]=> string(2)"21" ["subject"]=>string(12) "软件工程" } }
At this time, $students is an array and can be used directly:
for($i=0;$i<count($students);$i++){ echo "姓名:".$students[$i]['name']."年龄:".$students[$i]['age']."专业:".$students[$i]['subject']."<br/>"; }
The output result is:
Name: Zhang Xuemei Age: 27 Major: Computer Science and Technology
Name: Zhang Peilin Age: 21 Major: Software Engineering
Summary:
Two methods to process JSON formatted strings in PHP code:
Method one:
$json= '[{"id":"1","name":"\u5f20\u96ea\u6885","age":"27","subject":"\u8ba1\u7b97\u673a\u79d1\u5b66\u4e0e\u6280\u672f"},{"id":"2","name":"\u5f20\u6c9b\u9716","age":"21","subject":"\u8f6f\u4ef6\u5de5\u7a0b"}]'; $students= json_decode($json);//得到的是 object foreach($studentsas $obj){ echo "姓名:".$obj->name." 年 龄:".$obj->age." 专 业:".$obj->subject."<br />";}
Method two:
$json= '[{"id":"1","name":"\u5f20\u96ea\u6885","age":"27","subject":"\u8ba1\u7b97\u673a\u79d1\u5b66\u4e0e\u6280\u672f"},{"id":"2","name":"\u5f20\u6c9b\u9716","age":"21","subject":"\u8f6f\u4ef6\u5de5\u7a0b"}]'; $students= json_decode($json, true);//得到的是 array for($i=0;$i<count($students);$i++){ echo "姓名:".$students[$i]['name']." 年 龄:".$students[$i]['age']." 专 业:".$students[$i]['subject']."<br />";
----------------------------------------- -------------------------------------------------- -------------------------------------
The above is the detailed content of How to convert php json string to array. For more information, please follow other related articles on the PHP Chinese website!

1、先看看效果图,可以自行选择展示效果2、这是我在vue3项目中使用的JSON编辑器,首先引入第三方插件npminstalljson-editor-vue3yarnaddjson-editor-vue33、引入到项目中//导入模块importJsonEditorVuefrom'json-editor-vue3'//注册组件components:{JsonEditorVue},4、一般后端返回的是会将JSON转为String形式我们传给后端也是通过这种形式,就可以通

控制json序列化/反序列化1.@JsonIgnoreProperties的用法@JsonIgnoreProperties(value={"prop1","prop2"})用来修饰Pojo类,在序列化和反序列化的时候忽略指定的属性,可以忽略一个或多个属性.@JsonIgnoreProperties(ignoreUnknown=true)用来修饰Pojo类,在反序列化的时候忽略那些无法被设置的属性,包括无法在构造子设置和没有对应的setter方法.2.@Js

Java调用接口获取json数据保存到数据库1.在yml文件中配置自己定义的接口URL//自己定义的JSON接口URLblacklist_data_url:接口URL2.在Controller中添加请求方法和路径/***@Title:查询*@Description:查询车辆的记录*@Author:半度纳*@Date:2022/9/2717:33*/@GetMapping("/Blacklist")publicvoidselectBlacklist(){booleana=imB

本篇文章给大家带来了关于JWT的相关知识,其中主要介绍了什么是JWT?JWT的原理以及用法是什么?感兴趣的朋友,下面一起来看一下吧,希望对大家有帮助。

PHP作为一种常见的编程语言,在web开发中使用广泛,其与前端交互的方式也多种多样。其中,输出Json数据是一种常见的交互方式,但有时候会碰到Json无法解析的问题。为什么会出现无法解析的情况呢?下面列举了几个可能的原因。

JSONSchemaJSONSchema是用于验证JSON数据结构的强大工具,Schema可以理解为模式或者规则。JsonSchema定义了一套词汇和规则,这套词汇和规则用来定义Json元数据,且元数据也是通过Json数据形式表达的。Json元数据定义了Json数据需要满足的规范,规范包括成员、结构、类型、约束等。JSONSchema就是json的格式描述、定义、模板,有了他就可以生成任何符合要求的json数据json-schema-validator在java中,对json数据格式的校验,使用

当我们处理数据时经常会遇到将XML格式转换为JSON格式的需求。PHP有许多内置函数可以帮助我们执行这个操作。在本文中,我们将讨论将XML格式转换为JSON格式的不同方法。

一、@RestController注解在SpringBoot中的Controller中使用@RestController注解即可返回JSON格式的数据。@RestController注解包含了@Controller和@ResponseBody注解。@ResponseBody注解是将返回的数据结构转换为JSON格式。@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Controller@Respons


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools
