Skip to content

Instantly share code, notes, and snippets.

@ridinghoodmedia
Last active June 15, 2016 17:42
Show Gist options
  • Save ridinghoodmedia/354a542b6e3aeb26b3e0d079134c50b3 to your computer and use it in GitHub Desktop.
Save ridinghoodmedia/354a542b6e3aeb26b3e0d079134c50b3 to your computer and use it in GitHub Desktop.
PHP array functions
<?php
// Use array_search to get the key and remove it with unset if found:
if (($key = array_search('strawberry', $array)) !== false) {
unset($array[$key]);
}
// array_search returns false (null until PHP 4.2.0) if no item has been found.
// And if there can be multiple items with the same value, you can use array_keys to get the keys to all items:
foreach (array_keys($array, 'strawberry') as $key) {
unset($array[$key]);
}
//If you want to re-index starting to zero, simply do the following:
$iZero = array_values($arr);
//If you need it to start at one, then use the following:
$iOne = array_combine(range(1, count($arr)), array_values($arr));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment