PHP array is a list or map that contains multiple values grouped by array key. It may be including integer, string, boolean, or whatever data type, the key can be string or numeral index.

The PHP array can only have a value without keys but behind the scene, it is already indexing with numerals starting from 0 and ending with the count of array elements minus 1.

   key  => value,
   key1 => value2

   key => value,
   key => value2

The PHP array key can be a string or integer. And also can contain values without key

   $array = array(
     4 => 5,
     "false" => "string value",
     "1.5" => -55,
     "Hello World",
     "array" => array( "key" => "value" ),

How to Access The PHP Array Values

Entirely, if you look at the above example and count all elements of the array there, you will see each array value has a key name and some other values have no keys. But these values dynamically are taking a default index. The question is: how to 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.

   echo $array["false"];  // output: string value

But there are some other values that have no keys so to access them just you have to know the default index of the key in the array. Or use the print_r function to print the array with all keys

For example, if we need to access the fourth element in the above array "Hello World", and if you look there you will see the default index already appeared with the PHP predefined function print_r. And to reach that value just count the elements of the array after the last decimal number already started on the array like in the below image.

PHP Array Indexes

So the index of the needed value is 5 and 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 already assigned the 4 number into the array key, So all numbers already assigned for non-key values will count up after the 4 decimal number.

Otherwise, how do reset the PHP array to be started from the zero index? PHP already has a predefined function to reindex array keys, it calls array_values

    $arr = array_values($array);

This function will reindex the array keys to start from the zero index.

How to Add an Element to The PHP Array

To fill a PHP array, you have to know which index number or key name of the array to fill it. For example, if we have an empty array and we need to fill the first value with a float data type.

The logical answer is, that we know any array already has to start with a zero index. So to assign a value to the empty array use [0] or keep it empty [] then set the “=” operator after that assign the value.

$empty = array();

// Assign Values 
$empty[0] = 54.5;

// OR
$empty[] = 54.5;

// OR 
$empty[count($empty)] = 54.5;

To learn more details go to the PHP data Types tutorial.

Delete Element From The Array

To delete a specific value from the array, You have to use the “unset ” PHP function. this callback can remove any element in the array or delete the whole array.

  // Remote An Element From The Array
  unset( $arr[0] );
  // Delete The Whole Array 
  unset( $arr ); 

Detection for an element in The PHP Array

To check if a specific element already exists or not you have to use a predefined function “in_array”. It takes two parameters, the value, and an array.

   $array = array( "david", "michael", "codedtag" );
   echo in_array( "codedtag", $array ); // true

The in_array function already returns a boolean value, so it returns a positive value if the target value already exists in the array otherwise, if it doesn’t exist it returns a negative boolean value.