1

I am trying to delete an array whereby one of its values..(time) meet a specific condition. \The code I'm currently working with looks like this:

foreach($_SESSION as $key) {
   foreach($key['time'] as $keys=>$value){
      if(condition){
      unset($key);
   }
}
}

The array looks like this.

Array
    (
[form1] => Array
    (
        [hash] => lFfKBKiCTG6vOQDa8c7n
        [time] => 1401067044
    )

[form5] => Array
    (
        [hash] => TTmLVODDEkI1NrRnAbfB
        [time] => 1401063352
    )

[form4] => Array
    (
        [hash] => XCVOvrGbhuqAZehBmwoD
        [time] => 1401063352
    )

I tried to adapt solutions from these pages but didn't work.

Remove element in multidimensional array and save

PHP - unset in a multidimensional array

PHP How to Unset Member of Multidimensional Array?

4 Answers 4

2

If you want to unset the values inside it, a simple single foreach will suffice. Consider this example:

$values = array(
    'form1' => array('hash' => 'lFfKBKiCTG6vOQDa8c7n', 'time' => 1401067044),
    'form5' => array('hash' => 'TTmLVODDEkI1NrRnAbfB', 'time' => 1401063352),
    'form4' => array('hash' => 'XCVOvrGbhuqAZehBmwoD', 'time' => 1401063352),
);
$needle = 1401067044;
foreach($values as $key => &$value) {
    if($value['time'] == $needle) {
        // if you want to remove this key pair use this
        unset($values[$key]['time']);
        // if you just want to remove the value inside it
        $value['time'] = null;
        // if you want to remove all of this entirely
        unset($values[$key]);
    }
}

Fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

I get Notice: Undefined index: time in C:\wamp\www\email.php on line 54 on line if($value['time'] == $needle) {
@dotman14 maybe a mismatch on your array values as it works okay here (codepad.viper-7.com/eY7M2f) (also added fiddle on anwser)
2

Unsetting in a for loop can lead to issues, its easier and better to use array_filter which is optimized for this kind of problem. Here is how to do it with your example. ideone running code

<?php


$ar = Array(
  "form1" => Array
  (
    "hash" => 'lFfKBKiCTG6vOQDa8c7n',
    "time" => '1401067044'
   ),
  "form5" => Array
  (
    "hash" => 'TTmLVODDEkI1NrRnAbfB',
    "time" => '1401063352'
   ),
  "form4" => Array
  (
    "hash" => 'XCVOvrGbhuqAZehBmwoD',
    "time" => '1401063352'
   )
);

$condition = '1401067044';
$newArray = array_filter($ar, function($form) use ($condition) {
    if (!isset($form['time'])) {
      return true;
    }
    return $form['time'] != $condition;
});

var_export($newArray);

array_filter

Comments

1

Assuming your values are stored in $_SESSION

foreach($_SESSION as $key => $value) {
    if(isset($value['time']) && $value['time'] < 1401063352) {
        unset($_SESSION[$key]);
    }
}

If you are storing your values in $_SESSION you may want to consider storing them in a subfield like $_SESSION['myForms'] so if you need to add other values to your session you can easily access only the values you need.

3 Comments

There are fields there already $_SESSION['form1'], $_SESSION['form2'] etc
@dotman14 suppose you want to store some other information in your session, i.e. $_SESSION['user_id'], then it would be useful if all your forms were stored under $_SESSION['myForms'] i.e. $_SESSION['myForms']['form1'], $_SESSION['myForms']['form2'] etc. so you could iterate over only forms and not all session values
Okay, I get it. The solution about deleted all arrays.
1

You need to do

unset($_SESSION[$key])

However as mentioned by Victory, array_filter is probably a better approach to this.

4 Comments

PHP does not always duplicate an array it is about to iterate over.
Please read the following: nikic.github.io/2011/11/11/… - nikic is a PHP core developer. "Still some people don’t like to use it, because they think it is slow. One reason usually named is that foreach copies the array it iterates."
Sometimes it does some copying, not always. C.f. nikic.github.io/2011/11/11/…
Removed all references to array being copied. Thanks for the heads-up.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.