如何在php数组

时间:2016-10-09 10:37:30

标签: php mysql arrays session-variables

$matrix=array($_SESSION['review_buffer_name'],$_SESSION['review_buffer_mail'],$_SESSION['review_buffer_comment']);

上面的代码行在WHILE循环中,因此它存储了多个数组记录。它是否正确存储记录的方式?我们如何访问矩阵的每个记录和值?

$ matrix应该存储多行数组...问题是当我访问$ matrix [2]然后它给出数组的第二个值...而不是第二个数组记录

1 个答案:

答案 0 :(得分:0)

您可以尝试一下:

//Before while loop declare the array variable
$matrix = array();

While(your condition){
    $matrix[] = array(
           $_SESSION['review_buffer_name'],
           $_SESSION['review_buffer_mail'],
           $_SESSION['review_buffer_comment']
   );
}

//To access array:

print_r($matrix[0]); //print_r whole first row. (array start from 0)
echo $matrix[0][0]; //echo single data that first row's first data
  

或者您可以将索引设置为名称,如:

//Before while loop declare the array variable
$matrix = array();

While(your condition){
    $matrix[] = array(
           'review_buffer_name'=>$_SESSION['review_buffer_name'],
           'review_buffer_mail'=>$_SESSION['review_buffer_mail'],
           'review_buffer_comment'=>$_SESSION['review_buffer_comment']
    );
    }

//Then access array:

print_r($matrix[0]); //print_r whole first row. (array start from 0)
echo $matrix[0]['review_buffer_name']; // first row's first data