Necuima Posted May 3, 2014 Share Posted May 3, 2014 Hi, Can someone please tell me what the best way is to remove a key-value pair from a single-dimension array where you know the value but not the key. (In PHP). Any advice will be most appreciated. Thanks, Necuima. Link to comment Share on other sites More sharing options...
HartleySan Posted May 3, 2014 Share Posted May 3, 2014 Honestly, I think that a quick Google search would get you there, but array_search will return the key that corresponds to the first instance of a value in an array, and array_splice allows you to remove one or more elements from an array. For example: $arr = array('red', 'green', 'blue', 'yellow'); $key = array_search('blue', $arr); // Returns 2. array_splice($arr, $key, 1); // $arr is now array('red', 'green', 'yellow'). You could easily combine these two steps into an easy-to-use function as well: function remove_val_from_arr($val, &$arr) { array_splice($arr, array_search($val, $arr), 1); } $arr = array('red', 'green', 'blue', 'yellow'); remove_val_from_arr('red', $arr); // $arr is now array('green', 'blue', 'yellow'). That all make sense? Link to comment Share on other sites More sharing options...
Necuima Posted May 3, 2014 Author Share Posted May 3, 2014 Many thanks - I had just found the array_search approach via Google as you responded. Sorry to have bothered you! Link to comment Share on other sites More sharing options...
Necuima Posted May 4, 2014 Author Share Posted May 4, 2014 Hi HartleySan, I also found this approach in Stack Overflow courtesy "Bojangles": if(($key = array_search($value, $arr)) !== false) { unset($arr[$key]); } There was also a comment that the array_splice approach may not work in every circumstance. In your experience, is there any appreciable difference in the approaches? Re earlier, I just had not come across the array_search PHP function before and an earlier Google search did not reveal it. Cheers from Oz. Link to comment Share on other sites More sharing options...
HartleySan Posted May 4, 2014 Share Posted May 4, 2014 Could you please link to the comment that said that array_splice may not work in all cases? Also, there's a big difference between array_splice and unset: array_splice resets the array keys, unset does not. Link to comment Share on other sites More sharing options...
Necuima Posted May 4, 2014 Author Share Posted May 4, 2014 Hi HartleySan, From: http://stackoverflow.com/questions/7225070/php-array-delete-by-value-not-key : This (array_diff) only works for objects that can be converted to a string - @nischayn22 It's worth noting that for some reason array_diff uses (string) $elem1 === (string) $elem2 as its equality condition, not $elem1 === $elem2 as you might expect. The issue pointed out by @nischayn22 is a consequence of this. If you want something to use as a utility function that will work for arrays of arbitrary elements (which might be objects), Bojangle's answer might be better for this reason - Mark Avery. Cheers from Oz. Link to comment Share on other sites More sharing options...
HartleySan Posted May 4, 2014 Share Posted May 4, 2014 There's nothing in Bojangles's answer about why unset is better than array_splice. In fact, there's only one mention of array_splice on the entire page, and that guy links to a detailed explanation of why array_splice is better than unset. Thoughts? Link to comment Share on other sites More sharing options...
Necuima Posted May 4, 2014 Author Share Posted May 4, 2014 Hi HartleySan, As I said right at the very beginning, I was seeking advice on the best way to delete a key-value pair when you only know the value. Your advice and Google have helped me achieve my goal. I don't have enough detailed knowledge to make an informed judgement as to which way is best. I followed the lead http://www.programmerinterview.com/index.php/php-questions/how-to-delete-an-element-from-an-array-in-php/ which is the lead that I think you are referring to and right at the bottom is Bojangles approach. But as always I do appreciate your advice and insights. Cheers from my ageing grey cells here in Oz :-) Link to comment Share on other sites More sharing options...
Recommended Posts