PHP中的json_encode方法 [英] json_encode method in PHP

查看:170
本文介绍了PHP中的json_encode方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了一个json_encode函数的示例.关于1部分非常困惑:

Hi I recently came across an example of json_encode function. Very confused about 1 part:

<?php 

$runners=array{

'fname'=>5
'you' => 6
};

echo json_encode (array("runners"=>$runners));

?>

问题是,为什么最后一行的代码不能简单地是:

Question is, why can't the code on the last row simply be:

echo json_encode ($runners);

谢谢

推荐答案

首先,您的数组被错误地使用.它将使用小括号()而不是卷曲的{}.因此,您的数组将变为:

Firstly your array is wrogly used. It will use small brackets () not curly {}. So your array will become :

$runners=array(
  'fname' => 5,
  'you'   => 6
);

现在,当您将json_encode()设置为:echo json_encode ($runners);时,您将获得输出:

Now when you do json_encode() as: echo json_encode ($runners); you will get the output:

{"fname":5,"you":6}

如果您这样做:echo json_encode (array("runners"=>$runners));,您将获得输出:

And if you do : echo json_encode (array("runners"=>$runners)); you will get output:

{"runners":{"fname":5,"you":6}}

这篇关于PHP中的json_encode方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆