Delete an array value by another array

Brecht Schepens

Okay, I really don't have any idea on how to explain this.

I have a session array:

$_SESSION['users']['currentuser']['username'] = 'stijn';

this array is build dynamically. So, i need to remove some values of this array also dynamically.

for this, I have a function:

function removeSessionValue($keys) {
                $keys = explode(':', $keys);
                var_dump(array_keys($_SESSION));
                $tempArray = array();
                $reference = &$tempArray;
                foreach ($keys as $key) {
                    $reference[$key] = array();
                    $reference = &$reference[$key];
                }
                $multiArray = $tempArray;
            }

function call= removeSessionValues('users:currentuser:username');

So now I have the originarray (session) and the array to check if the session exists (built by the function).

Is there any way, on how I can unset the $_SESSION['user']['currentuser']['username'] ?

Important note, we don't know what values will be passed in the function, as also we don't know what sessions exists, as everything is ultradynamic ...

Arivan Bastos

A simple way is use eval():

function deepUnset(&$array, $keys)
{
    $cmd = 'unset($array["'.implode($keys, '"]["').'"]);';
    eval($cmd);
}

// Example:
$_session = array(
    'users' => array(
        'currentUser' => array(
            'username' => 'stijn',
        ),      

        'otherUser' => array(
            'username' => 'james',
        ),
    )
);

echo '<pre>';
print_r($_session);
deepUnset($_session, array('users', 'currentUser', 'username'));
print_r($_session);
echo '</pre>';

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Decrease the array value when another array is called

分類Dev

how to delete array value from table in jquery

分類Dev

Match values to nearest value in another array in R

分類Dev

Convert a value in the json array into another using JOLT

分類Dev

Sort [key => value] array by another array of keys with same index

分類Dev

Sorting an array based on property value of another array of objects

分類Dev

Get the value from array on the basis of another property array in AngularJS

分類Dev

Check array value exist in another array value that contain comma separated value

分類Dev

Delete Words from a String Array based on Words of Another String Array in C Programming

分類Dev

How to delete array of element in array

分類Dev

Delete element from multi-dimensional numpy array by value

分類Dev

How to delete a value from a dynamic array from any position in VBA?

分類Dev

delete an array element inside array of array

分類Dev

The value in the array?

分類Dev

Typescript Filter Array By Another Array

分類Dev

Group an array to match another array

分類Dev

JQuery add another value to each array item & sum

分類Dev

Accesing object value in array of objects by conditioning another object property

分類Dev

How alternately give value from one array to another js

分類Dev

anglejs select ng-options default value with another array

分類Dev

Populating one char array using fscanf changes the value of another char array

分類Dev

fastest way to replace values in an array with the value in the same position in another array if they match a condition

分類Dev

How can I get a new array filtered by value in another array of objects? javascript

分類Dev

Display values from one associative array whose key exists as the value in another array

分類Dev

Delete Array Element From Multidimensional Infinity Array

分類Dev

Delete main array when nested array is empty

分類Dev

Mapping array of objects into new array of array by value

分類Dev

How to change certain numpy array's target value to another value (if a list of target values is available)?

分類Dev

How to delete an element in an array with RethinkDB

Related 関連記事

  1. 1

    Decrease the array value when another array is called

  2. 2

    how to delete array value from table in jquery

  3. 3

    Match values to nearest value in another array in R

  4. 4

    Convert a value in the json array into another using JOLT

  5. 5

    Sort [key => value] array by another array of keys with same index

  6. 6

    Sorting an array based on property value of another array of objects

  7. 7

    Get the value from array on the basis of another property array in AngularJS

  8. 8

    Check array value exist in another array value that contain comma separated value

  9. 9

    Delete Words from a String Array based on Words of Another String Array in C Programming

  10. 10

    How to delete array of element in array

  11. 11

    Delete element from multi-dimensional numpy array by value

  12. 12

    How to delete a value from a dynamic array from any position in VBA?

  13. 13

    delete an array element inside array of array

  14. 14

    The value in the array?

  15. 15

    Typescript Filter Array By Another Array

  16. 16

    Group an array to match another array

  17. 17

    JQuery add another value to each array item & sum

  18. 18

    Accesing object value in array of objects by conditioning another object property

  19. 19

    How alternately give value from one array to another js

  20. 20

    anglejs select ng-options default value with another array

  21. 21

    Populating one char array using fscanf changes the value of another char array

  22. 22

    fastest way to replace values in an array with the value in the same position in another array if they match a condition

  23. 23

    How can I get a new array filtered by value in another array of objects? javascript

  24. 24

    Display values from one associative array whose key exists as the value in another array

  25. 25

    Delete Array Element From Multidimensional Infinity Array

  26. 26

    Delete main array when nested array is empty

  27. 27

    Mapping array of objects into new array of array by value

  28. 28

    How to change certain numpy array's target value to another value (if a list of target values is available)?

  29. 29

    How to delete an element in an array with RethinkDB

ホットタグ

アーカイブ