PHP 会话数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4301203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP session array
提问by hgbso
how can i store this array into a session and use sessions to move the elements inside the array up/down/left/right diagonally
如何将此数组存储到会话中并使用会话将数组内的元素向上/向下/向左/向右对角移动
$board = array(A B C D E F G H
0 array(0,0,0,0,0,0,0,0),
1 array(0,0,0,0,0,0,0,0),
2 array(0,0,0,0,0,0,0,0),
3 array(0,0,0,0,0,0,0,0),
4 array(0,0,0,0,0,0,0,0),
5 array(0,0,0,0,0,0,0,0),
6 array(0,0,0,0,0,0,0,0),
7 array(0,0,0,0,0,0,0,0)
);
I am trying to store this array into a session
我正在尝试将此数组存储到会话中
$pieces = array(
//checkers pieces player 1
"b" => '<img src="bp.png" width="33" height="37" alt="black piece">',
//Checkers pieces for player2
"r" => '<img src="rp.png" width="33" height="32" alt="red piece">',
// Empty Squares
// Black
"bs" => '<img src="bs.png" width="30" height="30" alt="black square">',
// Red
"rs" => '<img src="rs.png" width="30" height="30" alt="black square">'
);
// 'es' represents empty squares
$board = array( A B C D E F G H
0 array('b','rs','b','rs','b','rs','b','rs'),
1 array('rs','b','rs','b','rs','b','rs','b'),
2 array('b','rs','b','rs','b','rs','b','rs'),
3 array('rs','bs','rs','bs','rs','bs','rs','bs'),
4 array('bs','rs','bs','rs','bs','rs','bs','rs'),
5 array('r','bs','r','bs','r','bs','r','bs'),
6 array('bs','r','bs','r','bs','r','bs','r'),
7 array('r','bs','r','bs','r','bs','r','bs')
);
function map(&$value, $key, $map) {
if(array_key_exists($value, $map)) {
$value = $map[$value];
}
}
array_walk_recursive($board, 'map', $pieces);
and its going to come out into an 8x8 table board when it prints out
当它打印出来时,它会变成一个 8x8 的桌面
I did $_SESSION['board'] = $board;
after the array_walk_recursive
我$_SESSION['board'] = $board;
在 array_walk_recursive 之后做了
and put it into
并将其放入
echo "<table border='1'>\n";
foreach ($_SESSION['board'] as $row)
{
echo "<tr>\n";
foreach ($row as $piece){
echo "<td>";
echo "$piece ";
echo "</td>\n";
}
}
echo "</tr>\n";
echo "</table>\n";
}
the user is inputing into this function (FROM input box) F5 - (TO Input) G2 the parses it into coordinates with this function
用户正在输入此函数(FROM 输入框)F5 -(TO 输入)G2 使用此函数将其解析为坐标
// parses the users input --FROM-- and to where the user wnats to move the piece
// if the user inputs F1 it parses that into (0,0) coordinates
function parseSquare() {
if (strlen($square) != 2) {
return FALSE;
}
$coords = array(ord('A') - ord($square[0]),
$square[1] - 1);
// Perform bounds-checking.
if ($coords[0] < 0 || $coords[0] > 7 || $coords[1] < 0 || $coords[1] > 7) {
return FALSE;
}
return $coords;
}
$coords = parseSquare($square);
if ($coords === FALSE) {
// Invalid input, handle this case.
} else {
$piece = $board[$coords[0]][$coords[1]]; // for example
}
can i use the function above to move diagonally
我可以使用上面的功能对角移动吗
$_SESSION['board'][[$new_i]-1][[$new_j] + 1] = $_SESSION['board'][$old_i][$old_j];
$_SESSION['board'][$old_i][$old_j] = ...;
回答by casablanca
Call session_start
and afterwards store your variables in $_SESSION
-- they will be available throughout the session:
调用session_start
并随后将您的变量存储在$_SESSION
- 它们将在整个会话期间可用:
session_start();
$_SESSION['board'] = array( ... );
Moving elements is just a matter of assigning one value to another, for example:
移动元素只是将一个值分配给另一个值,例如:
$_SESSION['board'][$new_i][$new_j] = $_SESSION['board'][$old_i][$old_j];
$_SESSION['board'][$old_i][$old_j] = ...;
回答by qasimzee
$_SESSION['myArray'] = $board;
$_SESSION['myArray'] = $board;
and you can access any element using $_SESSION['myArray'][i][j];
您可以使用 $_SESSION['myArray'][i][j]; 访问任何元素;
回答by JAL
You store it in a session like
你将它存储在一个会话中
<?php
session_start();
$board=array('whatever');
$session['board']=$board;
As for manipulation, it is just a normal array. You can work with it like any other array.
至于操作,它只是一个普通的数组。您可以像使用任何其他数组一样使用它。
回答by hgbso
Yes. you can store and update an array in session. use like this :
是的。您可以在会话中存储和更新数组。像这样使用:
session_start();
$_SESSION['youarray'] =$board;
and now do updates in $_SESSION['youarray'] array according to your requirement that will be like normal array. but stored in session.
现在根据您的要求在 $_SESSION['youarray'] 数组中进行更新,就像普通数组一样。但存储在会话中。