0

I have the following string Available:

$flatPath = '0/instances/0';

With this information I want to be able to unset the element from a multidimensional array like this:

$array = [['instances' => [1,2], ['instances' => [3,4]]

Therefor the Result should look like:

$array = [['instances' => [2], ['instances' => [3,4]]

Statically it would look like:

unset($array[0]['instances'][0]);

I want to be able to remove items in any given string path if available in the array. Therefore the solution has to be able to remove any item from a multidimensional array, irrespective of the structure of the $array, as long as the $flatPath exists.

Thank you in advance.

3
  • 3
    Perhaps stackoverflow.com/questions/9627252/… would be a starting point. Commented Jan 7, 2021 at 12:04
  • I tried the code snippet, but nothing happened at all. Thanks anyways. Commented Jan 7, 2021 at 12:23
  • 1
    As I mentioned - it's a starting point, you would need to amend the code to do the exact thing you need. Commented Jan 7, 2021 at 12:32

2 Answers 2

2

Using the code from https://stackoverflow.com/a/9627299/1213708 as a starting point, this code follows the process with a few tweaks.

Exploding using / to split the string into it's parts, then it removes the last key so it can unset() that particular value from the layer above. Then it loops through the keys to find the layer above, and unsets the appropriate value...

$keys = explode('/', $flatPath);
$endKey = array_pop($keys);
$arr = &$array;
foreach ($keys as $key) {
    $arr = &$arr[$key];
}
unset($arr[$endKey]);
unset($arr);
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. But the result is not $array = [['instances' => [2], ['instances' => [3,4]]
What do you get as the output?
@Sandor Farkas The provided solution produces the desired outcome - what exactly do you mean But the result is not...?
Great job @NigelRen ! Thank you, i checked the wrong $variable as output....
0

Try this

function check_diff_multi($array1, $array2){
    $result = array();
    foreach($array1 as $key => $val) {
        if(array_key_exists($key,$array2)){
            if(is_array($val) && is_array($array2[$key]) && !empty($val)){
                $result[$key] = $this->check_diff_multi($val, $array2[$key]);
            }
        } else {
            $result[$key] = $val;
        }
    }
    return $result;
}



        $dane = array(array('instances' => [1,2]), array('instances' => [3,4]));
        $flatPath = '0/instances/0';
        
        $test = array();
        foreach (array_reverse(explode('/', $flatPath)) as $value)
        {
            if(empty($test))
            {
               $test = array($value => '');
            }
            else
            {
                $test = array($value => $test);
            }
        }
        
        var_dump(check_diff_multi($dane, $test));
 

method check_diff_multi comes from https://stackoverflow.com/a/12261512/14912402

2 Comments

You might want to add the source @fdrv of the function you're using.
Bat @Sandor Farkas the author of the post does not respond :(

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.