简体   繁体   中英

How to remove value from Multidimensional Array based on Key and Value - PHP

How can I remove value "288" from "pecan" and return the updated array.

Array like so:

a: 10: {
s: 13: "Fraser Island";
a: 7: {
    i: 0;
    i: 303;
    i: 1;
    i: 34;
    i: 2;
    i: 27;
    i: 3;
    i: 291;
    i: 4;
    i: 293;
    i: 5;
    i: 37;
    i: 6;
    i: 288;
}
s: 5: "pecan";
a: 5: {
    i: 0;
    i: 34;
    i: 1;
    i: 27;
    i: 2;
    i: 291;
    i: 3;
    i: 59;
    i: 4;
    i: 288;
}
s: 11: "New Triper";
a: 3: {
    i: 0;
    i: 291;
    i: 1;
    i: 293;
    i: 2;
    i: 288;
}

}

Which translates to this in PHP:

Array ( 
[0] => Array ( 
    [Fraser Island] => Array ( 
        [0] => 303 
        [1] => 34 
        [2] => 27 
        [3] => 291 
        [4] => 293 
        [5] => 37 
        [6] => 288 
    )
    [pecan] => Array ( 
        [0] => 34 
        [1] => 27 
        [2] => 291 
        [3] => 59 
        [4] => 288 
    ) 
    [New Triper!] => Array ( 
        [0] => 291 
        [1] => 293 
        [2] => 288 
    )
)

)

Delete/remove a value from a multidimensional array by finding the array column name and the value. I use this code:

// DELETE VALUE FROM ARRAY
function remove_element_by_value($arr, $val) {
    $return = array(); 
    foreach($arr as $k => $v) {
        if(is_array($v)) {
            $return[$k] = remove_element_by_value($v, $val); //recursion
            continue;
        }
        if($v == $val) continue;
            $return[$k] = $v;
        }
   return $return;
}

However it removes all matching values from all array columns.

So you know you are looking under the pecan key and you know you are looking for 288 , so just search to return the key and unset() :

$key = 'pecan';
$val = '288';

unset($array[0][$key][array_search($val, $array[0][$key])]);

To re-index:

$array[0][$key] = array_values($array[0][$key]);

Or to unwind it:

$found = array_search(288, $array[0]['pecan']);
unset($array[0]['pecan'][$found]);
$array[0]['pecan'] = array_values($array[0]['pecan']);

Try remove_element_by_value($arr, '288', 'pecan')

   // DELETE VALUE FROM ARRAY
    function remove_element_by_value($arr, $val, $keyToSearch) {
            $keyOfValue = array_search($val, $arr[0][$keyToSearch]);
            unset($arr[0][$keyToSearch][$keyOfValue]);
            $arr[0][$keyToSearch] = array_values($arr[0][$keyToSearch]); #Reindex
            return $arr;
    }

This way, you can delete the elements by giving it the key and value that you want to remove such as remove_element_by_value($arr, '288', 'Fraser Island') // remove 288 from Fraser Island etc

   // Code for PHPfiddle
function remove_element_by_value($arr, $val, $keyToSearch) {
        $keyOfValue = array_search($val, $arr[0][$keyToSearch]);
        unset($arr[0][$keyToSearch][$keyOfValue]);
        $arr[0][$keyToSearch] = array_values($arr[0][$keyToSearch]);
        return $arr;
}

$array = array(array('abc'=>array(1,2,3,4,5,6)));

print_r(remove_element_by_value($array, 2, 'abc'));
// DELETE VALUE FROM ARRAY
function remove_element_by_value($arr, $key ,$val ) {
    $return = array(); 
    foreach($arr as $k => $v) {
        if(is_array($v)) {
            $return[$k] = remove_element_by_value($v, $val); //recursion
            continue;
        }
        if($v == $val && $k == $key) continue;
            $return[$k] = $v;
        }
   return $return;
}

This function will remove Element From specific Indexed/Associative array:

function removeElement($array, $value) {
  if(is_array($array)) {
    $key = array_search($value,$array);
       if(!isset($key)) {
         unset($array[$key]);
         return $array;    
       } 
  }
}

This function will remove each element if the value matches from a multidimensional array :

function removeElementMulti($array, $value) {
   if(is_array($array)) {
      foreach($array as $array_key => $simple_array) {
         $key = array_search($value,$simple_array);
         if(isset($key)) {
            unset($array[$array_key][$key]);
         }
      }

      return $array;  
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM