Home >Backend Development >PHP Tutorial >PHP uses session two-dimensional array instance, session two-dimensional array instance_PHP tutorial
The example in this article describes the use of session two-dimensional array in PHP. Share it with everyone for your reference. The details are as follows:
The most common usage: a variable name
I hope this article will be helpful to everyone’s PHP programming design.
Let me give you an example directly. Remember to add points directly....
a.php
session_start();
$sql = "SELECT * FROM [TABLE] WHERE ..";
$data=array();
$query = mysql_query( $sql , $link );
while($row=mysql_fetch_array ($query, MYSQL_ASSOC )){
$data[$row['id']] = array('name' => $row['name'] , 'price' => $row['price ']);
}
//The $data obtained at this time is similar to $data=array( 6 => array('name' => 'aa','price' => 100 ) , 16 => array('name' => 'bb','price' => 102 ) );
session_register("data");
echo "go b";
?>
b.php
session_start();
print_r($HTTP_SESSION_VARS ['data']);
?>
The code is very simple. Haha.
$_SESSION['cart'][$isbn] is a two-dimensional array. For example:
cart represents a person's shopping cart, and
$isbn represents an item.
Now when I go shopping, this cart represents me.
I bought a computer and a TV. $isbn represents a computer or a TV. It depends on whether it is a computer or a TV. The value passed by $_POST["save"].
If what is passed is a computer, then $_SESSION['cart'][$isbn]="computer".