简体   繁体   English

如何在php中访问json数组?

[英]How to access a json array in php?

I'm using this html and javascript to send json data to my php file. 我正在使用此html和javascript将json数据发送到我的php文件。 http://jsfiddle.net/ExpertSystem/9aWNj/ http://jsfiddle.net/ExpertSystem/9aWNj/

How to access it from there in php to echo out a given element? 如何从那里访问它在php中回显给定的元素?

Something along the lines of: 类似于以下内容:

$value = json_decode($_POST["newOrder"])
echo $value[1];

etc 等等

I'm not sure how to retrieve data from this. 我不确定如何从中检索数据。

Here is a jsfiddle to see how the javascript should be in case you're still lost: http://jsfiddle.net/9aWNj/3/ 这是一个jsfiddle,用于查看在您仍然迷路的情况下应该如何使用javascript: http : //jsfiddle.net/9aWNj/3/

this is your data decoded by php: 这是您的数据由php解码:

`stdClass Object ( [order] => Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 [4] => 6 [5] => 7 [6] => 8 [7] => 9 [8] => 10 [9] => 11 [10] => 1 [11] => 12 ) )`

To access it it will be: example: 要访问它,将是:示例:

 $order_0=$value->order[0];
 $order_1=$value->order[1];

Or you use the true for json_decode and it will become like this 或者您对json_decode使用true,它将变成这样

 $order_0=$value['order'][0];
 $order_1=$value['order'][1];

Try this, It may helpful 试试这个,可能会有所帮助

$FP=fopen(JSON_DIR."JsonArray.txt",'r');
$J_ARRAY=fread($FP,filesize(JSON_DIR."JsonArray.txt"));
$J_ARRAY=json_decode($J_ARRAY,JSON_FORCE_OBJECT);

try this : 尝试这个 :

$value = json_decode($_POST["newOrder"], true)
echo "<pre>";
print_r($value);

Note the second parameter true , that gives you output as array else it will be object. 请注意第二个参数true ,它为您提供输出为数组,否则将成为对象。

Check the php manual. 检查PHP手册。

json_decode returns an object as default. json_decode返回一个默认对象。 use a second param true to return an array. 使用第二个参数true返回一个数组。

json_decode($_POST["newOrder"], true)

To use json in PHP like I think you might want to then I suggest you put a true in the json_decode function. 像我想在PHP中使用json一样,我建议您在json_decode函数中输入true。

This will give you the following: 这将为您提供以下内容:

$json = json_decode($data,true); 
$json["sub-values"]["sub thing"];

Why to send the JSON as a body of POST request? 为什么要发送JSON作为POST请求的主体? Do it simpler: 做得更简单:

$.ajax({
    url: "<url_to_php_file>",
    type: "POST",
    data: { order: JSON.stringify(dataArr) }
});

And on the server side use: 并在服务器端使用:

$value = json_decode($_POST["order"]);
echo $value[1];

Json_decode return result as object by default, to get array you need to put second parameter to true. Json_decode默认将返回结果作为对象返回,要获取数组,您需要将第二个参数设置为true。

$data = json_decode($_POST["newOrder"], false); $ data = json_decode($ _ POST [“ newOrder”],false); return result as object 返回结果作为对象

for printing object use 用于打印对象

echo $data->something; echo $ data->某物;

$data = json_decode($_POST["newOrder"], true); $ data = json_decode($ _ POST [“ newOrder”],true); return result as array 以数组形式返回结果

for printing array use 用于打印阵列

echo $data['something']; echo $ data ['something'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM