0

I have an GLOBAL array that keeps all the configurations, it looks like this:

$_SOME_ARRAY = array(
                'some_setting' => array(
                                    'some_value' => '1',
                                    'other' => 'value'
                                ),  
                'something_else' => 1,
            );

How can I delete keys from this array, using some function like:

deleteFromArray('some_setting/other')

I have tried different things, but can't seem to find a way, to delete it without manually calling unset($_SOME_ARRAY['some_setting']['other'])

EDIT

I have tried working on with it. The only solution I see so far, is by "rebuilding" the original array, by looping through each value and verify. The progress:

        public static function delete($path) {
            global $_EDU_SETUP;

            $exploded_path = explode('/', $path);

            $newConfig = array();

            $delete = false;

            foreach($exploded_path as $bit) {

                if(!$delete) {
                    $loop = $_EDU_SETUP;
                } else {
                    $loop = $delete;
                }

                foreach($loop as $key => $value) {

                    if($key == $bit) {
                        echo 'found first: ' . $key . '<br />'; // debugging

                        if(!$delete) {
                            $delete = $_EDU_SETUP[$key];
                        } else {
                            $delete = $delete[$key];
                        }
                    } else {
                        $newConfig[$key] = $value;
                    }
                }
            }

            $_EDU_SETUP = $newConfig;
        }

The array could look like this:

$array = array(
    'a' => array(
        'a',
        'b',
        'c'
    ),
    'b' => array(
        'a',
        'b',
        'c' => array(
            'a',
            'b',
            'c' => array(
                'a'
            ),
        ),
    )
);

And to delete $array['b']['c'] you would write Config::delete('b/c'); - BUT: It deletes whole B. It is only supposed to delete C.

Any ideas?

4
  • why don't you just split the sting with explode and use the params in your deleteFromArray function ? by the way, you could use $_SESSION :) Commented May 12, 2014 at 11:01
  • does this array has only 2 demensions ? Commented May 12, 2014 at 11:07
  • No - unlimited amount Commented May 12, 2014 at 11:11
  • could you post a array with the result your trying to achieve ? :) Commented May 12, 2014 at 12:19

3 Answers 3

1

This what you can do, assuming the array has 2 levels of data.

 $_SOME_ARRAY = array(
                    'some_setting' => array(
                                        'some_value' => '1',
                                        'other' => 'value'
                                    ),  
                    'something_else' => 1,
                );

function deleteFromArray($param){
    global $_SOME_ARRAY ;
    $param_values = explode("/",$param);
    if(count($param_values) == 2 ){
        unset($_SOME_ARRAY[$param_values[0]][$param_values[1]]);
    }else{
        unset($_SOME_ARRAY[$param_values[0]]);
    }
}

deleteFromArray('some_setting/other');
print_r($_SOME_ARRAY);

You can modify the function to add more strict rules by checking if the key exists before doing unset using the function array_key_exists()

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

3 Comments

Well - I'd like not to assume anything, as the config array should be able to be as big as needed.
I meant 2D array i.e. array['one']['two']
I have edited my post with additional information.. Maybe that will help.
1

how do you like this ?

$_SESSION = $_SOME_ARRAY; // Btw it should be session from beginning...

function deleteFromArray($string)
{

    $array = explode("/",$sting);

    foreach($array as $arrA)
    {
        foreach($array as $arrB)
        {
            unset($_SESSION[$arrA][$arrB]);
        }
    }
}

now you could delete more than one entry like

deleteFromArray('some_setting/some_value/a_other_value')

but take care of using dim1array names in dim2array...

of corse you could add more foreach or make a recursiv function out of it to get deep in the array

1 Comment

I think we're getting close! However, I don't see how this solution could work with a multidimensional array, like: delete/this/deep/value/in/config.
0

do you want to delete particular array using a unique index(like a primary id)?, i would use a for loop to look for that particular index then delete that array...E.g delete array where the index = 1 , pls check above

foreach ($_SOME_ARRAY as $a => $key)//get all child arrays of '$_SOME_ARRAY '
{
   foreach($Key as $b => $key2)//get values of the child arrays
   {
        if($Key[0] == 1)// if the index at[0] equals 1
        {
            unset($arr[$a][$b]); //delete that array
        } 
   }
}

2 Comments

i made some edits to your post, even if looks like a kind of copy paste to my psot ^^
lol. not really, i had to code something lyk these sometime last week aswell for a shopping cart m creating, a user needs to delete a product that i saved into a session array, bt thanks man

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.