0

So I have 2 files. In file 1 I have a table and there I randomly select some fields and store (store in session) them in an array of 2D arrays. When I click on the cell I send this data to my file 2 where I want to check if I clicked on a randomly selected array or not and if I did, I want to remove this 2D array from an main array. But as soon as I click on one of the selected arrays, the array crashes.

File 1 PHP stuff immportant for this:

 session_start();
$_SESSION['arrays'] =  $stack ;

File 2 PHP:

session_start();

if (isset($_SESSION['arrays'])) {

    $stack = $_SESSION['arrays'];

    for ($i = 0; $i< count($stack);$i++){
        if($cooridnates == $stack[$i]){

            unset($stack[$i]);
            array_values($stack);
            $i--;
            $Result = true;
            break;
        }
    }
    $_SESSION['arrays'] =   $stack ;

I am suspecting the error might be in 2 things:

  1. count($stack) used, but I don't believe this is the main reason.

  2. The way I store session.

I have tried using manuals from W3Schools and official PHP website and also SOF, but with no use. But still, I am not sure if the array_values() and unset() is working correctly since the thing chrashes and I can't test it correctly.

I would appreciate any tips.

2
  • 1
    You don’t seem to have a closing } bracket for your for statement. Commented Mar 5, 2017 at 20:07
  • @DzmitryKushnarou Sorry, spelling mistake... Commented Mar 5, 2017 at 20:09

2 Answers 2

1

You need to assign the result of array_values($stack); back to the $stack variable.

$stack = array_values($stack);

There's also no need to use $i-- when you do this, since you're breaking out of the loop after you find a match.

Instead of a loop, you can use array_search():

$pos = array_search($coordinates, $stack);
if ($pos !=== false) {
    unset $stack[$pos];
    $Result = true;
    $stack = array_values($stack);
    $_SESSION['arrays'] = $stack;
}
Sign up to request clarification or add additional context in comments.

5 Comments

You sir are a genius. I was absolutely sure I was using this in a right way. I was using official PHP site for help, but I guess I didn't use it right. I always enjoy a great tip from a more experienced member, from which I can learn a lot.
@JakaStudent7 Very few functions modify arrays in place. The only common exceptions are the sorting functions and functions like array_push. In most other cases a new array is returned and you have to assign it somewhere.
is the "array_splice()" function used in the same way as array_values()? Cuz I was trying to use that in the first place. but it didn't work. So I went with unset and array_values.
array_splice() modifies the array -- that's the difference between it and array_slice().
Oh thank you. You are a true MVP. P.S. sorry that I said Barmer in the comment of the Jain post, but I just realized that I made a spelling mistake and can't fix it anymore
0

you can do this like that by using foreach loop:

session_start();
if (!empty($_SESSION['arrays'])) {
    foreach( $_SESSION['arrays'] as $key => $val){
       if($cooridnates == $val){
             unset($_SESSION['arrays'][$key]); // if you want this removed value then assign it a variable before unsetting the array
             $Result = true;
             break;
        }
    }
 }

4 Comments

@Jain This should also work, but since I am quite new to PHP etc. I think the solution from Barmer is much easier to implement and modify in the future.
This is missing the call to array_values() to renumber all the elements of the array after the element is removed.
@jaka if you using foreach loop no matters what $index you are using it iterates on each element whether an array having a single value on array(1=>5) and if you only want to unset the field from session the you can directory unset the field then why are taking one another variable and assigning its value twice.
@SudhanshuJain That is actually a great idea. I love how you exaplined this to me. Okey I will implement this and also use it in the future. Thank you. + Another thank you for the easy explenation.

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.