You down with PHP?

Part 3

Robert M Ricci
May 16 · 3 min read
Photo by James Harrison on Unsplash

This is my third installment of me journey with PHP. The first post I gave a little history of PHP. We dove into variables, assignment operators, and contacting with the (.) dot operator. You can read that article here. In the second one which you can read here, we dove into functions, both custom and built in. In this one we are going to go over array structure. I will give example on how to create them, as well as how to modify them and also the few differnt ways to display them.

As a referesher an array is just a collection of data, where each piece holds a numerical index number. These indexes start at zero and end at the last element. In PHP a standard array is known as an ordered array. PHP has two differnt ways of creating arrays. One uses the array(), keyword, and hte other uses bracket notation[]. Check out the examples below.


$bracket_collection = ["Fourth", "Fifth","Sixth"];
echo $bracket_collection[2]; //"Sixth"

To access an element in an array, all you need to do is call its index. So for the first array, I called index zero and “First” was returned because zero in the first index. In the second array I call two and “Sixth” was returned because it was the last index, even though their are three elements, the indexng starts at zero.

Now how can we modify an array? Well, their are a few differnt ways to both add and subtract elements from arrays. The first two we wil go over is array_pop() which removes the last element form an array and array_shift() which removes the first element from an array.

$bracket_collection = ["Fourth", "Fifth","Sixth"];
echo array_pop($bracket_collection);
//$bracket_collection("Fourth", "Fifth")

On the inverse of that their are also ways to add elements to arrays. We have array_push() that adds elements to the end of an array and array_unshift() that adds to the beginning of an array.

$bracket_collection = ["Fourth", "Fifth","Sixth"];
echo array_push($bracket_collection, "Sixth");
//$bracket_collection("Fourth", "Fifth", "Sixth")

PHP has another form of array which is associative array. Which aould be similiar to a hash in Ruby. The main distinction which these are the key=>value dynamic, whese instead of an index you have a keyword representing the value. Here is an example.

$also_star_wars = ["scoundrel" => "Han Solo", "bounty hunter" => "Boba Fett"];
echo $also_star_wars["bounty hunter"];
//"Boba Fett"

The last thing I want to go over is how to combine arrays. You would use what is known as the union operator(+). The thing to note here is that the second array is always append, meaning that it goes to the end. So becareful you orer if you want a specific order.

$also_star_wars = ["scoundrel" => "Han Solo", "bounty hunter" => "Boba Fett"];$final_star_wars = $star_wars + $also_star_wars;$final_star_wars = ["jedi" => "Luke Skywalker", "Sith" => "Darth Vader", "scoundrel" => "Han Solo", "bounty hunter" => "Boba Fett"];

That’s arrays in a nutshell. I would highly suggest digging into PHP’s documentation. It has a lot of great info, with a ton more detail that I can’t cram in here without making this super long.

Geek Culture

Proud to geek out. Follow to join our +1.5M monthly readers.