gpt4 book ai didi

php - 如何在php数组中存储字符串变量

转载 作者:行者123 更新时间:2023-11-30 22:11:36 26 4
gpt4 key购买 nike

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

上面这行代码是在WHILE循环中,所以它存储了多条数组记录。这样存储记录的方式正确吗?。我们如何访问矩阵的每条记录和值?

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

最佳答案

你可以试试:

//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

Or you can set index as name like:

//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

关于php - 如何在php数组中存储字符串变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39942636/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com