Home  >  Article  >  Backend Development  >  PHP uses session two-dimensional array instance, session two-dimensional array instance_PHP tutorial

PHP uses session two-dimensional array instance, session two-dimensional array instance_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:14:53966browse

php uses session two-dimensional array instance, session two-dimensional array instance

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

Copy code The code is as follows:
$_SESSION['user'] = 0;
echo $_SESSION['user'];

Using an array:
Copy code The code is as follows:
$_SESSION['user']['a'] = 1;
$_SESSION['user']['b'] = 2;
echo $_SESSION['user']['a'];
echo $_SESSION['user']['b'];

The advantage of this is that it can be grouped.

If available:
Copy code The code is as follows:
$_SESSION['admin']['a'] = 11;
$_SESSION['admin']['b'] = 22;

I hope this article will be helpful to everyone’s PHP programming design.

PHP about two-dimensional arrays in session

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.

[Teaching, php] How to understand this $_SESSION two-dimensional array?

$_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".

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/907284.htmlTechArticlephp uses session two-dimensional array instance, session two-dimensional array instance This article tells the example of php using session two-dimensional array usage. Share it with everyone for your reference. The details are as follows: The most common...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn