Free cookie consent management tool by TermsFeed Generator PHP Arrays | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Arrays

PHP Arrays

Last Updated on Mar 21, 2023

What is Array?

Array is a datatype that can hold more than one value. So instead of having multiple variables like this:

$person1 = 'pratham';
$person2 = 'vitto';
$person3 = 'simon';

You can hold all of them in one variable

$people = array('pratham','vitto','simon');

How to define an Array?

There are 2 ways to define an array.

1. array()

like we’ve seen above

$people = array('pratham','vitto','simon');

2. using [ ]

it's a more modern approach

$people = ['pratham','vitto','simon'];

array() and [] are not different at all.

Array Deconstrcution

Before we talk about different types of arrays let’s decontrstuct an array.

Each array has 3 main part

  1. the array itself as a whole
  2. the position of each value inside the array and we call it key
  3. value inside the array

If we use var_dump function in php (which is a way to output a variable in details) on people we get the following output

var_dump($people);
// array(3) { [0]=> string(7) "pratham" [1]=> string(5) "vitto" [2]=> string(5) "simon" }

what we are interested in is:

array(3): Length of array is 3. it has 3 values

[0]=> string(7) "pratham": the value "pratham" at position (key) 0

As you can see we didn't define any key for any of the values inside the array but php assigned a key/index for each value.

pratham at index 0, vitto at index 1 and simon at index 2.

Indexes start at 0 (like most of the other programming languages) so the first value is not at position 1. It is at position 0

Array Types

Now let’s talk about different types of array in php. There are 2 types of arrays in php

  1. indexed
  2. associative

1. Indexed

you have numbers as the key. This number will be automatically assigned or you can add it manually.

$people = ['pratham', 17 => "vitto"];
// array(2) { [0]=> string(7) "pratham" [17]=> string(5) "vitto" }

pratham got index 0 automatically and we assigned the index 17 to vitto manually.

we can get the value at specific key by calling $array[$index]; for example in order to get pratham I can do this:

$people[0];
// pratham

we can add a new value to the array like this:

$people[] = 'hassib';

this will automatically index hassib in the array we can manually add an index as well

$people[15] = 'hassib';

2. Associative

This one is like objects in JavaScript or dictionary in python So rather than having numbers and indexes for the key you assign names or anything you want for the keys

$people = [
    'twitter' => 'pratham',
    'web3' => 'vitto',
    'feedhive' => 'simon'
];
// array(3) { ["twitter"]=> string(7) "pratham" ["web3"]=> string(5) "vitto" ["feedhive"]=> string(5) "simon" }

we can get the value at specific key by calling $array[$key]; for example to get simon I can do this:

$people['feedhive'];
// simon

we can add a new value to the array like this:

$people['js'] = 'hassib';

https://youtu.be/_dDusFjI02Q

Conclusion

Now you know about arrays in PHP.

I recommend you to open a PHP file and define different types of arrays. try to get values from them and add new values to them.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • what are arrays
  • different types of array
  • defining an index array, adding values to it and getting values from it
  • defining an associative array, adding values to it and getting values from it

Category: programming

Tags: #php #array

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP execution time
Feb 15, 2023 programming

PHP execution time

Today we are going to talk about calculating execution time in PHP. Sometimes it’s a good idea to calculate the execution time of your functions and script as a whole. ...

3 Min Read Read More
PHP Trait
Mar 22, 2023 programming

PHP Trait

Today we are going to talk about traits in PHP. A class can extend only one class. But what if you wanted to inherit multiple classes and have their functionalities in your current class? ...

4 Min Read Read More
PHP Array Combine
Feb 14, 2023 programming

PHP Array Combine

Today we are going to talk about array combine in PHP. Array combine is another useful array function that creates an array by using one array for keys and another for its values. ...

3 Min Read Read More
PHP Array Chunk
Feb 14, 2023 programming

PHP Array Chunk

Today we are going to talk about array chunk function in PHP. Sometimes you have to deal with a very large array of data. we'll learn how to split the array in different chunks. ...

6 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...