Create an array and access array elements in PHP

In PHP, we can create indexed and associative arrays, where integer and string can be used as keys respectively.

Indexed array

You can use the function array() to create an array.

$animal = array("dog", "cat", "sheep", "rabbit", "pig");

In the above example, PHP automatically assigns a numeric index to each element. To print out the details of the array $animal, you can use the function print_r().

print_r($animal);

The above code will output:

Array ( [0] => dog [1] => cat [2] => sheep [3] => rabbit [4] => pig )

As you can see, the index starts from 0 and increase by 1 for each element. You can use the array[key] syntax to access array elements.

print_r($animal[2]);

Output:

sheep

Or you can define your own numeric index explicitly for each array elements.

$animal = array(5=>"dog", 8=>"cat", 9=>"sheep", 14=>"rabbit", 15=>"pig");
print_r($animal);
echo "<br>";
print_r($animal[14]);

The output will be:

Array ( [5] => dog [8] => cat [9] => sheep [14] => rabbit [15] => pig )
rabbit

Associative array

You can also create an array in an associative style, where you can use your own string as a key for each element. With this kind of array, you can store data like using a database.

$student = array("First Name"=>"Emma", "Last Name"=>"Brown", "Age"=>12, "Class"=>"6H");
print_r($student);
echo "<br>";
print_r($student["First Name"]);
echo "<br>";
print_r($student["Age"]);

In the above code snippet, the variable $student stores a student's personal data with different keys: "First Name", "Last Name", "Age" and "Class". The output will be:

Array ( [First Name] => Emma [Last Name] => Brown [Age] => 12 [Class] => 6H )
Emma
12

Loop through an array

To iterate over the whole array, PHP’s foreach loop is a convenient way.

$student = array("First Name"=>"Emma", "Last Name"=>"Brown", "Age"=>12, "Class"=>"6H");foreach ($student as $key => $value) {
echo "{$key}: {$value}<br>";
}

Output:

First Name: Emma
Last Name: Brown
Age: 12
Class: 6H

Create an array of objects

If you want to create an array of objects, you can do like this:

class Car {
public $brand;
public $model;
public $color;
}
$cars = array();
$cars[0] = new Car;
$cars[0]->brand = 'Honda';
$cars[0]->model = 'Jazz';
$cars[0]->color = 'Blue';
$cars[1] = new Car;
$cars[1]->brand = 'Toyota';
$cars[1]->model = 'Yaris';
$cars[1]->color = 'White';

In the above code snippet, class Car is defined followed by the initialization of the array of objects

$cars = array();

Here, we create two sample objects in $cars[0] and $cars[1]. To loop through this array of objects, you can do as follows:

foreach ($cars as $key => $obj) {
echo "{$key}: {$obj -> brand}<br>";
echo "{$key}: {$obj -> model}<br>";
echo "{$key}: {$obj -> color}<br>";
}

The output is:

0: Honda
0: Jazz
0: Blue
1: Toyota
1: Yaris
1: White

Thanks for reading! To find more programming tutorials, please visit: CodeBilby.com

--

--

--

I am a programmer and a technical writer. To find more programming tutorials, please visit my website: https://www.codebilby.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Enemy AI Progress Part 5

The Right Way to Use AWS CodeStar

Recreating Sol LeWitt’s Famous Art in JavaScript

Sol LeWitt’s A Square Divided Horizontally and Vertically into Four Equal Parts artwork in JavaScript

The Truflation Oracle Now on Polygon

CS371P Fall 2021: Cameron Courtney (Week of 8 Nov — 14 Nov)

Creating a reticle in Unity

Using Terraform to create secure IAP tunnels on GCP with conditional IAM policies!

.NET Core and RabbitMQ Part 2 — Communication via AMQP

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Yongyao Yan

Yongyao Yan

I am a programmer and a technical writer. To find more programming tutorials, please visit my website: https://www.codebilby.com

More from Medium

Laravel 8 Inner Join Query Example

Password and Confirm Password Validation in Laravel

Render a DateTime Object in Twig

Laravel 8 CRUD Tutorial