PHP会话数组 [英] PHP session array

查看:63
本文介绍了PHP会话数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何将这个数组存储到一个会话中,并使用会话将数组中的元素对角向上/向下/向左/向右移动

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);

,它在打印出来时会出现在8x8的桌面板上

and its going to come out into an 8x8 table board when it prints out

在array_walk_recursive之后我做了$_SESSION['board'] = $board;

I did $_SESSION['board'] = $board; after the array_walk_recursive

并放入

              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";

  }

用户正在输入此功能(FROM输入框)F5-(TO输入)G2使用此功能将其解析为坐标

the user is inputing into this function (FROM input box) F5 - (TO Input) G2 the parses it into coordinates with this function

// 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] = ...;

推荐答案

调用session_start,然后将变量存储在$_SESSION中-它们将在整个会话中可用:

Call session_start and afterwards store your variables in $_SESSION -- they will be available throughout the 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] = ...;

这篇关于PHP会话数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆