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?

Community
  • 1
  • 1
hello
  • 1,168
  • 4
  • 24
  • 59

4 Answers4

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

Victory
  • 5,811
  • 2
  • 26
  • 45
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

user1978142
  • 7,946
  • 3
  • 17
  • 20
  • I get `Notice: Undefined index: time in C:\wamp\www\email.php on line 54` on line `if($value['time'] == $needle) {` – hello May 26 '14 at 02:01
  • @dotman14 maybe a mismatch on your array values as it works okay here (http://codepad.viper-7.com/eY7M2f) (also added fiddle on anwser) – user1978142 May 26 '14 at 02:33
1

You need to do

unset($_SESSION[$key])

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

Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
  • PHP does not always duplicate an array it is about to iterate over. – Cully May 26 '14 at 01:34
  • Please read the following: http://nikic.github.io/2011/11/11/PHP-Internals-When-does-foreach-copy.html - 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." – Martin Konecny May 26 '14 at 01:36
  • Sometimes it does some copying, not always. C.f. http://nikic.github.io/2011/11/11/PHP-Internals-When-does-foreach-copy.html – Cully May 26 '14 at 01:38
  • 1
    Removed all references to array being copied. Thanks for the heads-up. – Martin Konecny May 26 '14 at 01:47
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.

FuzzyTree
  • 32,014
  • 3
  • 54
  • 85
  • There are fields there already `$_SESSION['form1']`, `$_SESSION['form2']` etc – hello May 26 '14 at 01:35
  • @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 – FuzzyTree May 26 '14 at 01:40
  • Okay, I get it. The solution about deleted all arrays. – hello May 26 '14 at 01:41