Last active
June 15, 2016 17:42
-
-
Save ridinghoodmedia/354a542b6e3aeb26b3e0d079134c50b3 to your computer and use it in GitHub Desktop.
PHP array functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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