Udara

Software Application

Blog Post

PHP Arrays – All The Things You Should Know About Arrays In PHP Part 01

PHP Arrays

What is an PHP Array ?

Arrays are a Special type of variable that you can store multiple types of data in a single variable in general. But keep in mind that arrays store data not permanently. And another important thing is when we are talking about arrays, all data should have the same data type. We cannot store multiple data which have different data types.
As an example, if we create an array to store the name of customers, we cannot add their age as an integer into the array. All the data should be string ( text ) for this case.
We can divide the arrays mainly into 2 types by considering the behavior of arrays such as single and multi dimensional arrays. In Today we are going to learn about single dimensional arrays.

Single Dimensional Array | 1D Arrays

When we are talking about this type of array we can divide this also in two main types called index array and associative array. The main difference between index and associative array is, use integers as the keys for index array and use strings as the keys for associative array.

01. Index Array

The index arrays are one main type of the arrays as I mentioned before and this type of array use integers to present their key index. That’s why we called it as index array.

Create an array

We can use array() function to create an array in PHP. There are many ways to create an array in PHP.
Type 01
<?php
	$users = array("Name 1", "Name 2", "Name 3");
?>
Type 02
<?php
	$users = array();
    $users[0] = "Name 1";
    $users[1] = "Name 2";
    $users[2] = "Name 3";
?>
Type 03 (if you need to add data to array loop through follow below way)
<?php
    $users = array();
    $sizeOfarray = 10;
    
    for($i = 0; $i < $sizeOfarray; $i++) {
      $users[$i] = $i;
    }
?>
All the above types of array creations return the exact same output as a result. You can select any type from above types to your arrays.

Display data

Actually there are many ways to retrieve or display data of a array. In here I will show you 2 ways to retrieve data from an array.
If you need to display single value of an array , follow below path.
<?php 
    $users = array("Name 1", "Name 2", "Name 3");
    echo “Hello ”. $users[0];
    //Output : Hello Name 1
?>
If you need to display the whole array at once, follow the loop through path.
<?php
    $users = array("Name 1", "Name 2", "Name 3");
    $sizeOfarray = count($users);
    
    for($i = 0; $i < $sizeOfarray; $i++) {
      echo $users[$i];
      echo "<br>";
    }
?>

02. Associative Array

In Associative array we use string as the keys of the array. As an example if we need to create an array with user names of users, we should have to follow below way in this case.

Type 01

<?php 
	$users = array("user1"=>"Alex", "user2"=>"Roy", "user3"=>"Joe");
?>
Type 02
<?php
    $users[“user1”] = "Alex";
    $users[“user1”] = "Roy";
    $users[“user1”] = "Joe";
?>
All the above types of array creations return the exact same output as a result. You can select any type from above types to your arrays.

Display data from Associative array.

Actually there are many ways to retrieve or display data of a array. In here I will show you 2 ways to retrieve data from an array.
If you need to display single value of an array , follow below path.
<?php 
    $users = array("user1"=>"Alex", "user2"=>"Roy", "user3"=>"Joe");
    echo “Hello ”. $users[“user1”];
    //Output : Hello Alex
?>
If you need to display the whole array at once, follow the loop through path.
<?php
    $users = array("user1"=>"Alex", "user2"=>"Roy", "user3"=>"Joe");
    	foreach($users as $key => $value) {
      echo "Key=" . $key . ", Value=" . $value;
      echo "<br>";
    }
?>

Push Array

push_array() is a function that you can use to insert one or more values into the already exist array.
<?php
    $users=array("user1","user2");
    array_push($users,"user3","user4");
    print_r($users);
?>

PHP Array Sort Functions

Sorting refers to ordering data in an increasing or decreasing fashion according to some linear relationship among the data items.

PHP Array Functions

FunctionDescription
array()Creates an array
array_change_key_case()Changes all keys in an array to lowercase or uppercase
array_chunk()Splits an array into chunks of arrays
array_column()Returns the values from a single column in the input array
array_combine()Creates an array by using the elements from one “keys” array and one “values” array
array_count_values()Counts all the values of an array
array_diff()Compare arrays, and returns the differences (compare values only)
array_diff_assoc()Compare arrays, and returns the differences (compare keys and values)
array_diff_key()Compare arrays, and returns the differences (compare keys only)
array_diff_uassoc()Compare arrays, and returns the differences (compare keys and values, using a user-defined key comparison function)
array_diff_ukey()Compare arrays, and returns the differences (compare keys only, using a user-defined key comparison function)
array_fill()Fills an array with values
array_fill_keys()Fills an array with values, specifying keys
array_filter()Filters the values of an array using a callback function
array_flip()Flips/Exchanges all keys with their associated values in an array
array_intersect()Compare arrays, and returns the matches (compare values only)
array_intersect_assoc()Compare arrays and returns the matches (compare keys and values)
array_intersect_key()Compare arrays, and returns the matches (compare keys only)
array_intersect_uassoc()Compare arrays, and returns the matches (compare keys and values, using a user-defined key comparison function)
array_intersect_ukey()Compare arrays, and returns the matches (compare keys only, using a user-defined key comparison function)
array_key_exists()Checks if the specified key exists in the array
array_keys()Returns all the keys of an array
array_map()Sends each value of an array to a user-made function, which returns new values
array_merge()Merges one or more arrays into one array
array_merge_recursive()Merges one or more arrays into one array recursively
array_multisort()Sorts multiple or multi-dimensional arrays
array_pad()Inserts a specified number of items, with a specified value, to an array
array_pop()Deletes the last element of an array
array_product()Calculates the product of the values in an array
array_push()Inserts one or more elements to the end of an array
array_rand()Returns one or more random keys from an array
array_reduce()Returns an array as a string, using a user-defined function
array_replace()Replaces the values of the first array with the values from following arrays
array_replace_recursive()Replaces the values of the first array with the values from following arrays recursively
array_reverse()Returns an array in the reverse order
array_search()Searches an array for a given value and returns the key
array_shift()Removes the first element from an array, and returns the value of the removed element
array_slice()Returns selected parts of an array
array_splice()Removes and replaces specified elements of an array
array_sum()Returns the sum of the values in an array
array_udiff()Compare arrays, and returns the differences (compare values only, using a user-defined key comparison function)
array_udiff_assoc()Compare arrays, and returns the differences (compare keys and values, using a built-in function to compare the keys and a user-defined function to compare the values)
array_udiff_uassoc()Compare arrays, and returns the differences (compare keys and values, using two user-defined key comparison functions)
array_uintersect()Compare arrays, and returns the matches (compare values only, using a user-defined key comparison function)
array_uintersect_assoc()Compare arrays, and returns the matches (compare keys and values, using a built-in function to compare the keys and a user-defined function to compare the values)
array_uintersect_uassoc()Compare arrays, and returns the matches (compare keys and values, using two user-defined key comparison functions)
array_unique()Removes duplicate values from an array
array_unshift()Adds one or more elements to the beginning of an array
array_values()Returns all the values of an array
array_walk()Applies a user function to every member of an array
array_walk_recursive()Applies a user function recursively to every member of an array
arsort()Sorts an associative array in descending order, according to the value
asort()Sorts an associative array in ascending order, according to the value
compact()Create array containing variables and their values
count()Returns the number of elements in an array
current()Returns the current element in an array
each()Deprecated from PHP 7.2. Returns the current key and value pair from an array
end()Sets the internal pointer of an array to its last element
extract()Imports variables into the current symbol table from an array
in_array()Checks if a specified value exists in an array
key()Fetches a key from an array
krsort()Sorts an associative array in descending order, according to the key
ksort()Sorts an associative array in ascending order, according to the key
list()Assigns variables as if they were an array
natcasesort()Sorts an array using a case insensitive “natural order” algorithm
natsort()Sorts an array using a “natural order” algorithm
next()Advance the internal array pointer of an array
pos()Alias of current()
prev()Rewinds the internal array pointer
range()Creates an array containing a range of elements
reset()Sets the internal pointer of an array to its first element
rsort()Sorts an indexed array in descending order
shuffle()Shuffles an array
sizeof()Alias of count()
sort()Sorts an indexed array in ascending order
uasort()Sorts an array by values using a user-defined comparison function
uksort()Sorts an array by keys using a user-defined comparison function
usort()Sorts an array using a user-defined comparison function
Ask any question that you have in the contact section. Thank you!