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

Courses