PHP : Array Functions Tutorial – Part 1

0saves

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

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Related Posts

About the Author: Abhishek Sanghvi

Hi Friends, my name is Abhishek Sanghvi and I am the founder of this “open source script” site MYPHPSCRIPTZ. I have been learning and practising PHP from the last 3 years. You could always contact me if you wish to have some code for your need. I would surely try and solve them for you at abhishek(at)myphpscriptz(dot)com

4 Comments + Add Comment

Leave a comment


eight + = seventeen

CommentLuv badge