接收JSON php中的所有值

接收JSON php中的所有值,php,json,Php,Json,我有以下JSON: { "thirdPartyForms": [ { "driverAddress": "Adress", "PolicyOwner": "nimo", "driverInsCompany": "lkjlkj", "driverName": "drvivcer name", "agentPhone": "kj", "carLicensing": "lkjkl",

我有以下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)中的所有值。

解决方案

为了清晰起见,移到顶部

尝试此操作以递归方式展平阵列

$d = json_decode($json, true);

$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($d));
foreach($it as $k => $v) {
  echo $k . " is " . $v . "\n";
}
原始答案

您的代码按预期工作,无法
回送
打印
数组,数据就在那里。而是这样做

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\u 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";
   }  
}

用“\n”表示$value,这是一个字符串操作,因此以字符串结束。我建议您打印您的$d,您将看到如何处理它,以及我如何访问所有$myArray值(甚至“array”值)?Jake N:10x。但我不想一个接一个地访问它。我想将它们全部保存在一个数组中,以便通过foreach loophmm访问它们我的代码没有按预期工作,因为您有嵌套的数组,但它应该表明我的观点?这就是我的问题所在。因为嵌套数组。我试图将它们插入一个数组中,这样我就可以通过简单的foreach访问所有字段。标记为“答案”;-)