dongsaolian8786 2010-11-29 05:58
浏览 38
已采纳

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

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

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

and put it into

              echo "<table border='1'>
";
  foreach ($_SESSION['board'] as $row)
    {




    echo "<tr>
";
    foreach ($row as $piece){
     echo "<td>";
     echo "$piece ";
     echo "</td>
";




     }

    }

   echo "</tr>
";
   echo "</table>
";

  }

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] = ...;
  • 写回答

4条回答 默认 最新

  • doomli3721 2010-11-29 06:03
    关注

    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] = ...;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等