在 PHP 中访问 JSON 对象元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16285314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:51:04  来源:igfitidea点击:

Accessing JSON object elements in PHP

phpjsonobject

提问by ellis

I have a JSON object that I'm POST'ING to PHP from ExtJS interface. I get the object from

我有一个 JSON 对象,我正在从 ExtJS 接口 POST'ING 到 PHP。我从

$json = $_POST["newUserInfo"];

The object will contain 3 arrays, which I can see if I do

该对象将包含 3 个数组,如果我这样做,我可以看到

var_dump(json_decode($json));

I need to take each array and build SQL queries from them. My first obstacle is getting the arrays out of the object, though this may be unnecessary. Here is the code block I'm working from:

我需要获取每个数组并从中构建 SQL 查询。我的第一个障碍是从对象中取出数组,尽管这可能是不必要的。这是我正在使用的代码块:

/*Variable passed in from the ExtJS interface as JSON object*/
$json = $_POST["newUserInfo"];
//$json = '{"USER":{"ID":"","FULL_USER_NAME":"Some Guy","ENTERPRISE_USER_NAME":"guyso01","USER_EMAIL":"[email protected]","USER_PHONE":"123-456-7890"},"PERMISSIONS":{"ID":"","USER_ID":"","IS_ADMIN":"true"},"SETTINGS":{"ID":"","USERS_ID":"","BACKGROUND":"default"}}';

//Test to view the decoded output
//var_dump(json_decode($json));

//Decode the $json variable
$jsonDecoded = json_decode($json,true);

//Create arrays for each table from the $jsonDecoded object
$user_info = array($jsonDecoded['USER']);
$permissions_info = array($jsonDecoded['PERMISSIONS']);
$settings_info = array($jsonDecoded['SETTINGS']);  

I'm not creating the arrays correctly. I've also tried

我没有正确创建数组。我也试过

$user_info = $jsonDecoded->USER;

and that doesn't work either. I'm sure I'm missing something easy here. Again, this may be unnecessary as I can probably access them directly. I need to build the query by looping through the array and appending each key to a string and each value to a string. So I'd end up with something like

这也不起作用。我确定我在这里遗漏了一些简单的东西。同样,这可能是不必要的,因为我可能可以直接访问它们。我需要通过遍历数组并将每个键附加到字符串并将每个值附加到字符串来构建查询。所以我最终会得到类似的东西

$query = "INSERT INTO USERS ($keyString) VALUES ($valueString);

Then I'd repeat the same process for PERMISSIONS and SETTINGS arrays. This is probably simple but I'm stuck here.

然后我会对 PERMISSIONS 和 SETTINGS 数组重复相同的过程。这可能很简单,但我被困在这里。

回答by Adidi

If you are using json_decode($json,true);- true means returning the js objects results as associative arrays - then all you have to do is $user_info = $jsonDecoded['USER'];without the array()cast cause that is what json_decodedo for you.

如果您正在使用json_decode($json,true);- true 意味着将 js 对象结果作为关联数组返回 - 那么您所要做的就是$user_info = $jsonDecoded['USER'];没有对您array()有用的强制转换原因json_decode

If you would choose to omit the second boolean parameter then you will get an stdClass which $jsonDecoded->USER;would work for you

如果您选择省略第二个布尔参数,那么您将获得一个$jsonDecoded->USER;适合您的 stdClass