0

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.

2
  • pass specific key and specific value to function. now you are checking value only Commented Jun 8, 2017 at 23:19
  • How about recreate array? $a = array("foo", "bar", "orange"); $a = 0; $a = array("foo", "bar"); Commented Jun 8, 2017 at 23:20

4 Answers 4

2

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']);
Sign up to request clarification or add additional context in comments.

5 Comments

It works, that is my question answered. But for some reason it changes the nature of the array, perhaps because of update_user_meta.... $user = wp_get_current_user(); $user = $user->ID; $meta = get_user_meta($user, 'fishingspots'); //ARRAY $key = $_GET['trip']; $val = $_GET['spot']; unset($meta[0][$key][array_search($val, $meta[0][$key])]); update_user_meta($user, 'fishingspots', $meta);
Okay, so now the array index for pecan misses a index number. If I remove value "34" from "pecan" the index starts at 1 and not 0. :|
OK, that shouldn't matter and wasn't a requirement but added array_values().
awesome thanks! I was just adding the new array wrong, it should have been: update_user_meta($user, 'fishingspots', $meta[0]);
Just a side not, this is the first time i have worked with multi arrays.
1

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'));

4 Comments

My unset statement was wrong, I edited it, should be working now.
Thanks, but it doesnt seem to work. And I dont think it would reindex the array after?
I have edit the code for reindexing. You can past this code to phpfiddle to verify how it works. Thank a lot.
Awesome thanks this works! I almost had it working before this question, but I was missing updating the database wrong. Thanks again for your help this function is great.
0
// 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;
}

Comments

0

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;  
   }
}

2 Comments

That would remove all matching values.
Yes, it would remove all the matching values

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.