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.

jibsteroos
  • 1,366
  • 2
  • 7
  • 13
Wolf-Tech
  • 1,259
  • 3
  • 18
  • 38
  • 3
    Perhaps https://stackoverflow.com/questions/9627252/php-make-multi-dimensional-associative-array-from-a-delimited-string would be a starting point. – Nigel Ren Jan 07 '21 at 12:04
  • I tried the code snippet, but nothing happened at all. Thanks anyways. – Wolf-Tech Jan 07 '21 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. – Nigel Ren Jan 07 '21 at 12:32

2 Answers2

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);
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
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

WiatroBosy
  • 1,076
  • 1
  • 6
  • 17
  • You might want to add the [source @fdrv](https://stackoverflow.com/questions/12246039/multidimensional-array-difference-php) of the function you're using. – jibsteroos Jan 07 '21 at 15:10
  • Bat @Sandor Farkas the author of the post does not respond :( – WiatroBosy Jan 07 '21 at 15:20