PHP Array
Last updated onPHP arrays are lists or maps that contain multiple values grouped by array keys. They may include integers, strings, booleans, or any other data type. The keys can be strings or numerical indexes.
A PHP array can have values without explicit keys. However, it is indexed with numerals starting from 0 and ending with the count of array elements minus 1.
Here is a quick example.
array(
key => value,
key1 => value2
)
[
key => value,
key => value2
]
Additionally, PHP array key can be either a string or an integer. It can also contain values without explicit keys.
Here is an example:
<?php
$array = array(
4 => 5,
"false" => "string value",
"1.5" => -55,
"Hello World",
"array" => array( "key" => "value" ),
55.654
);
?>
Anyway, in the following section, you will learn how to access array values. Let’s move on.
How to Access PHP Array Values
Entirely, if you look at the above examples and count all the elements of the arrays, you will see each array value has a key. Some values do not have explicit keys, but they are dynamically assigned default indexes. The question is: how do you access these values?
To access a key already found in the array, like in the third element (“false” => “string value”), you just need to call it with the key name.
<?php
echo $array["false"]; // output: string value
?>
But there are some other values that have no keys, so to access them, you just need to know the default index of the key in the array. Alternatively, you can use the print_r
function to print the array with all its keys.
For example, if we need to access the fourth element in the array “Hello World”, you will see the default index already appearing with the PHP predefined function print_r. To reach that value, just count the elements of the array starting from the last decimal number, as shown in the image below.
So, the index of the needed value is 5. To reach it, use the following code.
<?php echo $array[5]; ?>
But the array should start from a zero index, and in the previous example, it started from 4! That happened because we assigned the number 4 to the array key. So, all numbers assigned to non-key values will count up after the number 4.
Otherwise, how do you reset the PHP array to start from the zero index? PHP already has a predefined function to reindex array keys; it’s called array_values
.
It can be written as follows.
<?php
$arr = array_values($array);
?>
This function will reindex the array keys to start from zero.
Well done! You have learned how to access the array elements. But how do you add a new element to an array? Let’s move on to the section below to understand how you can do that.
Add an Element to the PHP Array
To fill or update a PHP array element, you need to know the index number or key name of the array. For instance, if we have an empty array and we need to fill the first value with a float data type, the logical approach is based on the fact that any array starts with a zero index.
Therefore, to assign a value to the empty array, you can either use [0]
or leave it empty []
, and then use the =
operator to assign the desired value. This method ensures that the array is properly initialized with the correct value at the specified index.
Here is an example:
$empty = array();
// Assign Values
$empty[0] = 54.5;
// OR
$empty[] = 54.5;
// OR
$empty[count($empty)] = 54.5;
But, if you decide to delete an unneeded element from the array, you also need to focus on the array key or index. Let’s move on to the following section to learn how to do that.
Delete an Element From the Array
Once you select the element index inside the array, you can use the PHP built-in function unset
. This will remove the element directly. But it doesn’t reindex the array.
For example:
<?php
$arr = [ "FY", "HP", "DI" ];
// Remote An Element From The Array
unset( $arr[0] );
print_r($arr);
/* Output:
Array
(
[1] => HP
[2] => DI
)
*/
?>
It seems like an incomplete array! As you know, the array should start with a zero index, but here it started with a 1 index, which can be confusing when used. Let’s reorder the array with array_values
.
<?php
$arr = [ "FY", "HP", "DI" ];
// Remote An Element From The Array
unset( $arr[0] );
$arr = array_values($arr);
print_r($arr);
/* Output:
Array
(
[0] => HP
[1] => DI
)
*/
?>
In the following part, you will learn how to check for an existing element inside the array. This helps you avoid errors. Let’s move on.
Array Element Detection in PHP
The isset()
function is used to check if a variable is set and is not null
. This can be used to check if a specific key exists in an array and its value is not null
.
Here is an example:
<?php
$fruits = array("a" => "apple", "b" => "banana", "c" => "orange");
if (isset($fruits["b"])) {
echo "Key 'b' is set in the array.";
} else {
echo "Key 'b' is not set in the array.";
}
?>
There are many built-in functions in PHP that serve similar purposes, and we will explain them in separate tutorials.
Anyway, let’s summarize it.
Wrapping Up
In this tutorial, you learned the basics of PHP arrays, which can store multiple values using either string or numerical keys. These arrays can hold different types of data, such as integers, strings, booleans, and more. Keys can be explicitly defined or automatically assigned, starting from 0.
Additionally, you discovered how to access array values by using their keys or default indexes. You saw how to reindex arrays using the array_values
function, which helps maintain a logical sequence of keys.
We also covered how to add new elements to an array, either by specifying the index or allowing PHP to assign it automatically. Furthermore, you learned how to remove elements using the unset
function and reindex the array to keep it tidy.
Finally, you learned to check for existing keys in an array using the isset
function, ensuring your code avoids errors by verifying the presence of specific keys.
Thank you for reading! Happy Coding.