简体   繁体   中英

How to store string variables in php array

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

The above line of code is inside WHILE loop So that it stores more than one record of array.Is it correct way to store records?. And how can we access each record and value of matrix?

$matrix should store multiple row of arrays... The problem is when i'm accessing $matrix[2] then it is giving second value of array... instead of second record of array

You can try it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM