访问多维$ _POST数组

时间:2015-12-02 01:12:39

标签: php arrays ajax multidimensional-array

从ajax电话中我发送了一些数据,如

rows[tr1][rowDesc]:ccccc
rows[tr1][rowTaxable]:77777
rows[tr1][rowVAT]:22% 
rows[tr1][rowTAX]:17110.94
rows[tr1][rowTOTAL]:94887.94
rows[tr2][rowDesc]:ddddd
rows[tr2][rowTaxable]:55555
rows[tr2][rowVAT]:22% 
rows[tr2][rowTAX]:12222.1
rows[tr2][rowTOTAL]:67777.1

使用print_r($_POST['rows']);给出以下多维数组

Array 
( 
[tr1] => Array
            ( 
              [rowDesc] => ccccc 
              [rowTaxable] => 77777 
              [rowVAT] => 22% 
              [rowTAX] => 17110.94 
              [rowTOTAL] => 94887.94 
            ) 
[tr2] => Array 
            ( 
              [rowDesc] => ddddd 
              [rowTaxable] => 55555
              [rowVAT] => 22% 
              [rowTAX] => 12222.1 
              [rowTOTAL] => 67777.1 
             )
 )

我尝试使用foreach循环访问子数组,这只给了我第一个。那么,如何访问各种" tr1,tr2,tr3,ecc ......"价值观和关键?

感谢您的任何建议。

2 个答案:

答案 0 :(得分:1)

您可以执行以下操作来访问子数组:

foreach($_POST['rows'] as $array){
    foreach($array as $key => $value){
        echo $key . " : " . $value . " ";
    }
    echo "<br />";
}

输出:

rowDesc : ccccc rowTaxable : 77777 rowVAT : 22% rowTAX : 17110.94 rowTOTAL : 94887.94
rowDesc : ddddd rowTaxable : 77777 rowVAT : 22% rowTAX : 12222.1 rowTOTAL : 67777.1 

答案 1 :(得分:1)

...试

foreach($_POST['rows'] as $key => $value){

    if(is_array($value)){

       foreach($value as $subkey => $subvalue){

          echo $subkey;
          echo $subvalue;

       }

    }else{

      echo $key;
      echo $value;

    }
}