0

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'>\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

// 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 Answers 4

4

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] = ...;
Sign up to request clarification or add additional context in comments.

4 Comments

@hgbso: I don't see any new question there.
$_SESSION['board'][$new_i -1][$new_j +1] = $_SESSION['board'][$old_i][$old_j]; $_SESSION['board'][$old_i][$old_j] = ...; can i use this to move diagonally
@hgbso: You can move from anywhere to anywhere. "Moving" the pieces is just setting the new location and emptying the old location.
its putting user input into that movement like F5 to G2 I updated the display of the array
2

$_SESSION['myArray'] = $board;

and you can access any element using $_SESSION['myArray'][i][j];

Comments

1

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.