Parsing a JSON file or string is just as easy as parsing XML once you get the syntax, and in this tutorial I’ll show you how to parse and learn its syntax. But before we begin let’s establish some naming conventions for PHP arrays just in case you don’t know them already.
Take a look at the following PHP array.
$arrayName[key]=value;
Arrays in PHP have three parts, the name or variable used, the key ( can be associative or numeric) and the value of that key.
When we read/parse a json string or file with PHP what we will be doing is basically assigning a bunch of keys to a variable and so that variable will become an associative array. In case you haven’t heard the terms associative or numeric, numeric simply means that you are using numbers as keys.
Now that we got a convention let’s look at a simple JSON string.
$string='{"name":"John Adams"}';
You can think of this string as a key “name” that holds the value “John Adams”. Notice the brackets, they are used to separate arrays of keys. So we can give the following naming convention to this json string
{"key":"value"}
To parse JSON with PHP we will be using the funcion json_decode , this function takes a json string for its first parameter and an optional boolean (true/false) for its second parameter. The second parameter, if set to true, returns the json string as an associative array, if it’s not set it will return an object. The main difference between the two options is the syntax you use to refer to keys, for now we’ll be setting the second parameter to true so that it returns the more familiar associative array, but we will cover objects.
Now that you know about this new function make your php file look like mine.
$string='{"name":"John Adams"}';$json_a=json_decode($string,true);
Using json_decode with the second parameter set to true we have assigned an associative array to the variable $json_a, so $json_a is now an array. I use “_a” to refer to an associative array, we’ll later use $json_o to refer to a variable that holds the json as an object but you can use whatever variable name you want.
Let me translate the code above to a more familar and basic snippet that is doing the same thing.
$json_a=array("name"=>"John Adams");// or also$json_a["name"]="John Adams";
Now it’s clear what you can do with “$json_a” right? To show contents of “name” you would simply echo $json_a[name] like this
echo $json_a[name];// and you should see the name John Adams printed on your browser.
That was easy, now let’s use the same json string but this time we won’t set the second parameter so that we can get an object. Fill your php file with the following code.
$string='{"name":"John Adams"}';$json_o=json_decode($string);
This time instead of returning an array, json_decode has returned an object. The word object might sound a little intimidating but believe me when I say this isn’t hard at all.
To show the contents of “name” all one has to do is this
echo $json_o->name;
Can you tell the difference between outputting $json_a and $json_o’s content? That’s right! the only difference is that with $json_o you get rid of the leading “]” and replace “[” with “->”.
echo $json_a[name];// andecho $json_o->name;// will both display “John Adams”
The json string we've been using is very simple so am going to make it a little more interesting. The content of your php file up to this point should be this
$string='{"name":"John Adams"}';// create object from string$json_o=json_decode($string);// create array from string$json_a=json_decode($string,true);
Whenever I make changes to the value of your $string variable keep the last two lines with json_decode for the rest of this tutorial.
$string='{ "name":"John Adams", "age":"40" }';$json_o=json_decode($string);$json_a=json_decode($string,true);
As you might guess from the the new way $string looks, you separate each set of keys and values with a comma. So to display to value of age, which is 40, you can use either of these two methods.
// associative array modeecho $json_a[age];// object modeecho $json_o->age;
But this JSON string is still too simple, why don't we assign two keys to a key. Then new json string will be this.
$string='{ "name":{"first":"John","last":"Adams"}, "age":"40" }';
What we’ve done is we have separated the name “John Adams” into two "subkeys;” , first and last. We can display the values of these new keys this way.
// array method // display first name echo $json_a[name][first]; // display last name echo $json_a[name][last];// object method // display first name echo $json_o->name->first; // display last name echo $json_o->name->last;
So far so good. You have probably seen the symbols [ and ] used in json and if you haven't you will right now because that's what our new $string variable will include.
$string='[ { "name":{"first":"John","last":"Adams"}, "age":"40" }, { "name":{"first":"Thomas","last":"Jefferson"}, "age":"35" } ]';
We use [ and ] to indicate that we have an array of keys, that is more than one set of { }'s. That means then, that this string has two arrays , each of them separated by { and } and followed by a comma except for the last one. This array is numeric, so to access "age" in the second array we'll do the following.
// remember that arrays start at zero, that's why we use 1 to refer to the second array// array methodecho $json_a[1][age];// object methodecho $json_o[1]->age;
In your web development career you will encounter yet another type of json string.
$string='{"person": [ { "name":{"first":"John","last":"Adams"}, "age":"40" }, { "name":{"first":"Thomas","last":"Jefferson"}, "age":"35" } ] }';
This string is just like the previous one except that we have added {"person": at the beginning and to close, we added a } at the end.
What we have here is two "person" arrays one for John and one for Thomas. You can display both last names with this snippet of code.
// array method // first person "0"echo $json_a[person][0][name][last]; // second person "1"echo $json_a[person][1][name][last];// object method // first person "0"echo $json_o->person[0]->name->last; // second person "1"echo $json_o->person[1]->name->last;
It all seems fine, until we have a 100 "person" array or more! But don't worry. Because these arrays are numeric we can use a loop to go through a huge array like that. I like to use "foreach" loops but you can use "while", "for" or "do while" loops if you want. The next snippet will show you how you can get each person's information using a foreach loop.
$string='{"person":[ { "name":{"first":"John","last":"Adams"}, "age":"40" }, { "name":{"first":"Thomas","last":"Jefferson"}, "age":"35" } ]}';$json_a=json_decode($string,true);$json_o=json_decode($string);// array methodforeach($json_a[person] as $p){echo 'Name: '.$p[name][first].' '.$p[name][last].'Age: '.$p[age].'';}// object methodforeach($json_o->person as $p){echo 'Name: '.$p->name->first.' '.$p->name->last.'Age: '.$p->age.'';}
Reading a JSON is just as easy and it does not have a special extension, the file just has to output a json string. To read a JSON file you can use the function file_get_contents like this $string=file_get_contents(filepath) . Now pass $string to json_decode and parse.
From:http://webhole.net/2009/08/31/how-to-read-json-data-with-php/

PHP在现代编程中仍然是一个强大且广泛使用的工具,尤其在web开发领域。1)PHP易用且与数据库集成无缝,是许多开发者的首选。2)它支持动态内容生成和面向对象编程,适合快速创建和维护网站。3)PHP的性能可以通过缓存和优化数据库查询来提升,其广泛的社区和丰富生态系统使其在当今技术栈中仍具重要地位。

在PHP中,弱引用是通过WeakReference类实现的,不会阻止垃圾回收器回收对象。弱引用适用于缓存系统和事件监听器等场景,需注意其不能保证对象存活,且垃圾回收可能延迟。

\_\_invoke方法允许对象像函数一样被调用。1.定义\_\_invoke方法使对象可被调用。2.使用$obj(...)语法时,PHP会执行\_\_invoke方法。3.适用于日志记录和计算器等场景,提高代码灵活性和可读性。

Fibers在PHP8.1中引入,提升了并发处理能力。1)Fibers是一种轻量级的并发模型,类似于协程。2)它们允许开发者手动控制任务的执行流,适合处理I/O密集型任务。3)使用Fibers可以编写更高效、响应性更强的代码。

PHP社区提供了丰富的资源和支持,帮助开发者成长。1)资源包括官方文档、教程、博客和开源项目如Laravel和Symfony。2)支持可以通过StackOverflow、Reddit和Slack频道获得。3)开发动态可以通过关注RFC了解。4)融入社区可以通过积极参与、贡献代码和学习分享来实现。

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP不是在消亡,而是在不断适应和进化。1)PHP从1994年起经历多次版本迭代,适应新技术趋势。2)目前广泛应用于电子商务、内容管理系统等领域。3)PHP8引入JIT编译器等功能,提升性能和现代化。4)使用OPcache和遵循PSR-12标准可优化性能和代码质量。

PHP的未来将通过适应新技术趋势和引入创新特性来实现:1)适应云计算、容器化和微服务架构,支持Docker和Kubernetes;2)引入JIT编译器和枚举类型,提升性能和数据处理效率;3)持续优化性能和推广最佳实践。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

记事本++7.3.1
好用且免费的代码编辑器

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SublimeText3 Linux新版
SublimeText3 Linux最新版