Grab the array id within the session

Asked

Viewed 405 times

0

I have the session $_SESSION['linha_pedagio'][] so disposed:

Array
(
    [0] => Array
        (
            [0] => 2
            [1] => CGO - RJA (via Magé)
            [2] => 96.30
            [3] => 2
            [4] => 9681
        )

    [2] => Array
        (
            [0] => 4
            [1] => RJA - CPQ
            [2] => 224.70
            [3] => 2
            [4] => 493
        )

    [3] => Array
        (
            [0] => 9
            [1] => RJA - BMA
            [2] => 72.00
            [3] => 1
            [4] => 5270
        )

    [4] => Array
        (
            [0] => 
            [1] => BMA X B. PIRAI
            [2] => 20.00
            [3] => 1
            [4] => 5027
        )

    [5] => Array
        (
            [0] => 2
            [1] => CGO - RJA (via Magé)
            [2] => 96.30
            [3] => 2
            [4] => 4493
        )

)

But I need to delete one of the $_SESSION arrays, which are 0, 2 or 5.

I used the unset($_SESSION['linha_pedagio'][$id]); but I collect the wrong value for the variable $id,

There is a way to get this main id?

Values: 0, 2, 3, 4 and 5.

1 answer

2


You can "capture" that id as follows:

foreach($_SESSION['linha_pedagio'] as $id => $objeto){
    if($id == 0 || $id == 2 || $id == 5){
        unset($_SESSION['linha_pedagio'][$id]);
    }
}
  • Thank you Julyano... doing this $id => $objeto can I still use the other data from the right array? Ex: $rota = $objeto[1]; $valor = $objeto[2];

  • 1

    Yes, that’s right @thiagofred

Browser other questions tagged

You are not signed in. Login or sign up in order to post.