Unravelling the Magic of PHP Arrays: Discovering the Top 12 Array Methods

berastis
3 min readMay 19, 2023

--

Hello coders! PHP is an immensely powerful language, particularly when it comes to handling arrays. But sometimes, we need to add a touch of creativity and fun to our code. So, let’s dive into the top 12 PHP array methods, spiced up with a few emojis! Embrace the journey, and by the end, you’ll be writing more expressive and enjoyable code. 🎉

1. array()

Creating an array is the first step in our journey. Here’s how we do it:

$myArray = array("🍏", "🍌", "🍒");

2. count()

To find out the number of elements in our array, we use count():

echo count($myArray); // Outputs: 3

3. in_array()

To check if a specific fruit is in our array, we use in_array():

if (in_array("🍌", $myArray)) {
echo "🍌 is in the array!";
}

4. array_push()

To add more fruits to the end of our array, we use array_push():

array_push($myArray, "🍓", "🍑");
// Array before: ["🍏", "🍌", "🍒"]
// Array after: ["🍏", "🍌", "🍒", "🍓", "🍑"]

5. array_pop()

To remove the last fruit from our array, we use array_pop():

$lastFruit = array_pop($myArray);
// Array before: ["🍏", "🍌", "🍒", "🍓", "🍑"]
// Array after: ["🍏", "🍌", "🍒", "🍓"]

6. array_shift()

To remove the first fruit from our array, we use array_shift():

$firstFruit = array_shift($myArray);
// Array before: ["🍏", "🍌", "🍒", "🍓"]
// Array after: ["🍌", "🍒", "🍓"]

7. array_unshift()

To add a fruit at the start of our array, we use array_unshift():

array_unshift($myArray, "🥝");
// Array before: ["🍌", "🍒", "🍓"]
// Array after: ["🥝", "🍌", "🍒", "🍓"]

8. sort()

To arrange our fruits in alphabetical order, we use sort():

sort($myArray);
// Array before: ["🥝", "🍌", "🍒", "🍓"]
// Array after: ["🍌", "🍒", "🍓", "🥝"]

9. array_merge()

To merge our array with another one, we use array_merge():

$moreFruits = array("🍇", "🍈");
$mergedArray = array_merge($myArray, $moreFruits);
// Array before: ["🍌", "🍒", "🍓", "🥝"]
// Array after: ["🍌", "🍒", "🍓", "🥝", "🍇", "🍈"]

10. array_slice()

To take a slice from our array, we use array_slice():

$slicedArray = array_slice($myArray, 1, 2);
// Array before: ["🍌", "🍒", "🍓", "🥝"]
// Array after: ["🍒", "🍓"]

11. array_map()

To append some text to each fruit in our array, we can use array_map(). Here, we append the string ' is tasty!' to each fruit:

$tastyArray = array_map(function($value) { return $value . ' is tasty!'; }, $myArray);
// Array before: ["🍌", "🍒", "🍓", "🥝"]
// Array after: ["🍌 is tasty!", "🍒 is tasty!", "🍓 is tasty!", "🥝 is tasty!"]

12. array_filter()

To filter out a specific fruit from our array, we use array_filter():

$filteredArray = array_filter($myArray, function($value) {
return $value != "🍌"; // Filters out "🍌" from the array
});
// Array before: ["🍌", "🍒", "🍓", "🥝"]
// Array after: ["🍒", "🍓", "🥝"]

And there you have it! This is the magic of PHP arrays. But remember, the true magic lies in applying these functions creatively to solve problems. Continue exploring and innovating. You're a wizard in the making! 🧙‍♀️🧙‍♂️ Happy coding! 🌟

--

--