1

I am a noob programmer so sorry in advance, but I have a problem with deleting a value out of my array.

Here I foreach my session wich is an array if multiple values are added, I check if it's an array if it is I loop trough it and echo it. I made a delete link that makes a get id(key).

 foreach ($session->get('results') as $num => $value) {
                if (!is_array($value)) {
                    echo $value . '<br>';
                } else {
                    $av = count($value);
                    for ($a = 0; $a < $av; $a++)
                        echo $value[$a] . '<a href="export.php?key='. $num .'">verwijder</a><br>';
                }

When the link is clicked it makes a get id, I check if get id is set. now I want to unset the value which has the same $key as the get id.

if (isset($_GET['key'])) {
                    if (is_array($value)) {
                            unset($value[$_GET['key']]);
                    }
                }

At this moment the get id gets created but it doesn't unset the value with the same key. Has someone an answer to this, or an other way to do this?

yorrid08
  • 19
  • 3
  • Have you tried `as $num => &$value) {`,using `&` is a reference to the value which allows you to change it. Although please remember to use `unset($value);` after the `foreach()` due to https://stackoverflow.com/questions/4969243/strange-behavior-of-foreach. – Nigel Ren Nov 09 '18 at 10:53
  • Just tried it, doesn't work but thanks for the quick response man! – yorrid08 Nov 09 '18 at 10:57
  • Can you show the full code for your last piece, how does this code fit in with the loop etc.? – Nigel Ren Nov 09 '18 at 10:58
  • if it's a Laravel use [forget()](https://laravel.com/docs/5.7/session#deleting-data) method – Agnius Vasiliauskas Nov 16 '18 at 13:48

1 Answers1

1
$session->get('results') as $num => $value

$num is your key of the element in $session->get('results')

your link -> <a href="export.php?key='. $num .'

if you want to delete the value with this key unset($session->get('results')[$_GET['key']])

Yaroslaw
  • 108
  • 4
  • I thought of this myself, but I get the same outcome as when I use `unset($value[$_GET['key']])` it still won't unset it – yorrid08 Nov 09 '18 at 17:28