我有以下JSON:
{
"thirdPartyForms": [
{
"driverAddress": "Adress",
"PolicyOwner": "nimo",
"driverInsCompany": "lkjlkj",
"driverName": "drvivcer name",
"agentPhone": "kj",
"carLicensing": "lkjkl",
"driverId": "0980980980",
"driverPhone": "098908098",
"agentName": "j"
},
{
"driverAddress": "",
"PolicyOwner": "",
"driverInsCompany": "",
"driverName": "driver 5",
"agentPhone": "",
"carLicensing": "",
"driverId": "",
"driverPhone": "",
"agentName": ""
}
],
"personalForm": {
"insDriverName": "",
"insCarLicensing": "",
"insId": "039956974",
"insName": "bnrus",
"insDriverId": "",
"insPhone": ""
}
}
我尝试使用我写的这个函数保存所有字段:
function JSONForm()
{
if(get_magic_quotes_gpc()){
$d = stripslashes($_POST['formJSON']);
}else{
$d = $_POST['formJSON'];
}
$d = json_decode($d,true);
$myArray = array();
foreach($d as $item)
{
foreach($item as $key => $value)
{
$myArray[$key] = $value."\n";
}
}
}
我没有得到所有的字段(我得到'数组'而不是字段)。这是我的输出:
0 => Array
1 => Array
insDriverName =>
insCarLicensing =>
insId => 039956974
insName => bnrus
insDriverId =>
insPhone =>
如何改进我的功能以使其有效? 这意味着 - >我无法保存JSONArray中的所有值(输出行1和2)。
答案 0 :(得分:0)
解决方案
为了清晰起见,移到了顶部。
尝试以递归方式展平数组
$d = json_decode($json, true);
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($d));
foreach($it as $k => $v) {
echo $k . " is " . $v . "\n";
}
原始回答
您的代码按预期工作,您不能echo
或print
数组,数据就在那里。而是这样做;
function JSONForm()
{
if(get_magic_quotes_gpc()){
$d = stripslashes($_POST['formJSON']);
}else{
$d = $_POST['formJSON'];
}
$d = json_decode($d,true);
// A var_dump here will show you the true values of all of the
// decoded array
var_dump($d);
// In fact you do not need to iterate over the array, at all
// because the variable $d already contains all the json data
$myArray = array();
foreach($d as $item)
{
foreach($item as $key => $value)
{
// This will output all the array using print_r, setting the 2nd
// parameter to be true will return the output, rather than print it
$myArray[$key] = print_r($value, true)."\n";
}
}
}
修改强>
这是您的数据;
// thirdPartyForms is an array, it has no keys so the array will be
// zero indexed
"thirdPartyForms": [
{ // this is position zero
"driverAddress": "Adress",
"PolicyOwner": "nimo",
"driverInsCompany": "lkjlkj",
"driverName": "drvivcer name",
"agentPhone": "kj",
"carLicensing": "lkjkl",
"driverId": "0980980980",
"driverPhone": "098908098",
"agentName": "j"
},
{ // this is position one
"driverAddress": "",
"PolicyOwner": "",
"driverInsCompany": "",
"driverName": "driver 5",
"agentPhone": "",
"carLicensing": "",
"driverId": "",
"driverPhone": "",
"agentName": ""
}
],
"personalForm": {
"insDriverName": "",
"insCarLicensing": "",
"insId": "039956974",
"insName": "bnrus",
"insDriverId": "",
"insPhone": ""
}
然后,您可以使用编号索引访问数组;
// This will give you 'Adress'
echo $d["thirdPartyForms"][0]["driverAddress"]
// This will give you 'bnrus'
echo $d["personalForm"]["insName"];
使用var_dump($d)
可以让您更轻松,因为您可以更清楚地看到阵列的组成。
使用循环;
foreach($d as $key => $data)
{
// This will output the data for a key, note that var_dump will
// output an array just fine
var_dump($data);
// The issue you have is that $data could be a string or another array,
// Because your first element "thirdPartyForms" is an array of arrays
// The second element "personalForm" is an array of strings
// Check if its an array and if so iterate again
if(is_array($data))
{
echo $key . " is an array\n";
// Here we loop over the array again, note that if you have arrays in a
// array then you will need a recursive function
foreach($data as $k => $value)
{
echo $k . " within an array is " . $value . "\n";
}
}
else
{
echo $key . " is not an array, it is " . $data . "\n";
}
}