0

I have different multidimensional arrays with different keys and values:

$array_1 = array('cars' => array('audi' => array('a3' => array('one', 'two', 'three'), 'a5' => array('five', 'six', 'seven')), 'mercedes' => array('type_1' => array('submodel' => array('whatever'))), 'other_cat' => array('different_things')));

then I would like to unset a specific key, for example:

unset($array_1['cars']['audi']['a5']);

Now I like to "split it up", to have the key variable.

$to_unset = ['cars']['audi']['a5']; 

How can I unset that specific (variable!) key?

Aäron

6
  • What do you mean by to have the key variable? Commented Nov 9, 2017 at 16:16
  • unset($array_1.$to_unset); //unset the $to_unset from $array_1 ... Commented Nov 9, 2017 at 16:20
  • 1
    The assignment $to_unset = ['cars']['audi']['a5']; is not quite what you want. It is valid PHP code but it evaluates to NULL (and triggers a notice about the 'audi' index not existing in the ['cars'] array). You should start by designing a way to define the list of keys you need to reach the item to unset. Commented Nov 9, 2017 at 16:26
  • What I want to do is to unset a multidimensional array from another multidimensional array, in this case it's ['cars']['audi']['a5'] but it also could be ['song']['a5'] or ['cars']['mercedes']['cars'], just matching on "a5" is not a solution in my case. Thanks for your help already Commented Nov 9, 2017 at 16:38
  • I've attempted to answer your question. Please let me know if there is anything unclear or if I did not fully answer it. Commented Nov 9, 2017 at 16:50

3 Answers 3

1

An easy utility to avoid removing array keys that do not exist by accident:

function removeArrayKey($path, &$array ) {

    $array_temp = &$array;
    $previousItem = null;

    $path_bits = explode( ".", $path );

        foreach( $path_bits as &$path_bit ) {

           if( !isset( $array_temp[ $path_bit ] ) ) {
                  die("Error" . $path_bit);
                   //throw new Exception( "Path does not exist in array" );
            }

            $previousItem = &$array_temp;
            $array_temp = &$array_temp[ $path_bit ];

        }

        if( isset( $previousItem ) ) {

             unset( $previousItem[ $path_bit ] );

        }

        return $array;

}

To use the function, simply use removeArrayKey( "cars.mercedes.cars", $array_1 ); and separate each array index with a .

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

6 Comments

This only works if the first key is cars or mercedes but not with multidimensional arrays, thanks for your help anyway!
It does? Simply change "cars.mercedes.cars" to "cars.audi" or whatever else you want... Please read my explanation carefully.
And what are you trying to say with that reply?
Comment box is to short, check my answer above in the question
I have fixed the issue.
|
0

So as i see your problem, you'd like to save your array path into a variable. You can solve this problem on two different ways:

Save every key into a variable

The way i would do it, if my array structure looks always the same (e.g. [cars][type][model]). You can save the key to delete into a variable:

$cars = 'cars';
$type = 'audi';
$model = 'a5';

unset($array_1[$cars][$type][$model]);

This will work excellent in a for(each) loop.

Saving your keys into an array

This method will save your problem, but it is not the best option. You can save all the keys you like to unset into an array. This way can cause many bugs and you should reconsider your array structure, if this way is your solution.

// arrays start at 0
$to_unset = [
    0 => 'cars',
    1 => 'audi',
    2 => 'a5',
];

unset($array_1[$to_unset[0]][$to_unset[1]][$to_unset[2]]);

An other possible option here is to name the keys of the $to_unset array.

// arrays start at 0
$to_unset = [
    'cars' => 'cars',
    'type' => 'audi',
    'model' => 'a5',
];

unset($array_1[$to_unset['cars']][$to_unset['type']][$to_unset['model']]);

1 Comment

This is a possible solution if you know the keys in advance, but that's not the case. Thanks anyway for the help
0

You can probably go with eval but it's not recommended.

eval('unset($array_1' . $to_unset . ');');

2 Comments

This really seems the only solution, it works! thanks a lot
As you said, do NOT use this unless you know 100% sure that the input is not malicious.

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.