PHP : Array Functions Tutorial – Part 1
Previously I had written articles about array and its functions, but that was limited as I had posted only those functions which are used the most.
You can refer those articles here :
What is an Array? Functions of Array- Array Tutorials
Function : Check if array index is set and whether it has a value.
Function: Get Random Element from Array
From now I will post almost all the functions of array and explain an example for the same. This is Part I of the tutorial.
Array Function – Part I
Lets start with the basic function
array()
This function is used to create an array.
Syntax:
array("index1"=>"value1", "index2"=>"value2",..."indexn"=>"valuen")
Example:
$array_example = array("apple","bat","cat");
In this code an array is created. As no index is specified the default indexing is done as 0,1,2….n
$array_example = array("a"=>"apple","b"=>"bat","c"=>"cat");
In this code array is created with index values as a for apple, b for bat and so on. Such type of arrays are called associative arrays.
array_keys()
This function returns all the keys of an array.
Syntax:
array_keys($array);
Example:
$array_example = array("a"=>"apple","b"=>"bat","c"=>"cat");
print_r(array_keys($array_example));
Output:
Array( [0]=>a [1]=>b [2]=>c )
array_chunk()
This function splits an array into chunks(multiple array). It returns a multidimensional array.
Syntax:
array_chunk($array , $size , $preserve_keys)
$array is an array.
$size is the size on the basis of which chunks will be made. $size is integer.
$preserve_keys is a boolean value(true or false) which specifies whether to preserve the key for the chunk or not. Default is false.
Note : Size of chunk should be less than size of array else warning will be thrown.
Example:
$array_to_chunk = array("a", "b", "c", "d", "e", "f", "g", "h");
print_r(array_chunk($input_array, 2));
Output:
Array ( [0] => Array ( [0] => a [1] => b [2] => c ) [1] => Array ( [0] => d [1] => e [2] => f ) [2] => Array ( [0] => g [1] => h ) )
Example:
$array_to_chunk = array("a", "b", "c", "d", "e", "f", "g", "h");
print_r(array_chunk($array_to_chunk, 2, true));
Output:
Array ( [0] => Array ( [0] => a [1] => b [2] => c ) [1] => Array ( [4] => d [5] => e [6] => f ) [2] => Array ( [7] => g [8] => h ) )
array_rand()
This function returns one or more random elements from array.
Syntax:
array_rand($array,$required_elements)
Example:
$array_all = array("a", "b", "c", "d", "e", "f", "g", "h");
$rand_values = array_rand($array_all,3);
echo $rand_values[0];
echo "<br>".$rand_values[1];
Output:
(Any random element. This is just an example) d b
array_count_values()
This function returns the count of occurance of each element of an array.
Syntax:
array_count_values($array)
Example:
$array_all = array("apple","bat","cat","apple","cat);
print_r(array_count_values($array_all));
Output:
Array( [apple]=>2 [bat]=>1 [cat]=>2 )
Suggestion, Comments, Improvements and Questions are always welcome.
Reference:
What is an Array? Functions of Array- Array Tutorials
Function : Check if array index is set and whether it has a value.
Function: Get Random Element from Array
php.net












[...] the rest here: PHP : Array Functions Tutorial – Part I | My PHP Scripts | A Hub … Share and [...]
[...] Read thіѕ article: PHP : Array Functions Tutorial – Pаrt 1 | Mу PHP Scripts | A Hub … [...]
[...] more here: PHP : Array Functions Tutorial – Part 1 | My PHP Scripts | A Hub … Share and [...]
Here output of the array_chunk() should be:
Array
(
[0] => Array
(
[0] => a
[1] => b
)
[1] => Array
(
[0] => c
[1] => d
)
[2] => Array
(
[0] => e
[1] => f
)
[3] => Array
(
[0] => g
[1] => h
)
)