PHP : Array Functions Tutorial – Part 2
This is continuation to Array Functions Tutorial.
Previous Articles can be read from here
PHP : Array Functions Tutorial – Part I
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
Array Function – Part II
array_flip()
This function flips between the keys and their values i.e. that keys becomes values and values becomes keys.
Syntax:
array_flip($array)
Example:
$array = ("a"=>"apple", "b"=>"bat", "c"="cat");
$new_array = array_flip($array);
print_r($new_array);
Output:
Array( [apple]=>a [bat]=>b [cat]=>c )
Example:
$array = ("a"=>"apple", "a"=>"ant", "c"="cat");
Output:
Array( [ant]=>a [cat]=>c )
array_key_exists()
This function check if the key/index in the array exists. It returns true if the key/index is found else false. This function does not works on array objects(from php version 5.3.0)
Syntax:
array_key_exists($key,$array);
Example:
$array = ("a"=>"apple", "b"=>"bat", "c"=>"cat");
if(array_key_exists("a",$array))
{
echo "key found"; // This is output
}
else
{
echo "key not found";
}
array_fill_keys()
This function fills/populates an array with specific value to the keys assigned.
Syntax:
array_fill_keys($array_keys,$string_value);
Example:
$keys = array("a","e","i","o","u");
$string = "vowel";
$new_array = array_fill_keys($keys,$string);
Output:
Array ( [a]=>vowel, [e]=>vowel, [i]=>vowel, [o]=>vowel, [u]=>vowel )
array_fill()
This function fills/populates an array with values based on the range of keys provided.
Syntax:
array_fill($start_index,$limit,$value);
If limit is less than 1 then E_WARNING is thrown
Example:
$array_1 = array_fill("3","4","php-array-tutorials");
$array_2 = array_fill("-3","4","php-array-tutorials-part-2");
print_r($array_1);
print_r($array_2);
Output:
Array( [3]=>php-array-tutorials [4]=>php-array-tutorials [5]=>php-array-tutorials [6]=>php-array-tutorials ) Array( [-3]=>php-array-tutorials-part-2 [0]=>php-array-tutorials-part-2 [1]=>php-array-tutorials-part-2 [2]=>php-array-tutorials-part-2 )
array_map()
This function applies a callback funtion to all the elements of the array
Syntax:
array_map($function, $array);
Example:
$array = array("apple","bat","cat");
$new_array = array_map("strtoupper",$array);
print_r($new_array);
Output:
Array( [0]=>APPLE [1]=>BAT [2]=>CAT )
array_unique()
This function removes duplicate entries from an array. Keys are preserved in the return value. If two value matches, the first occurance is used.
Syntax:
array_unique($array);
Example:
$array = array("fruit1"=>"apple","fruit2"=>"apple","fruit3"=>"mango","fruit4"=>"orange");
$new_array = array_unique($array);
Output:
Array( [fruit1]=>apple, [fruit3]=>mango, [fruit4]=>orange )
count()
This function counts the number of elements in an array or an object.
Syntax:
count($array)
Example:
$array = array("fruit1"=>"apple","fruit2"=>"apple","fruit3"=>"mango","fruit4"=>"orange");
echo count($array);
Output:
4
size_of()
This function is an alias of count(). You can refer to count() for more info.
Suggestion, Comments, Improvements and Questions are always welcome.
Reference:
PHP : Array Functions Tutorial – Part I
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
Related posts:
- PHP : Array Functions Tutorial – Part 3
- PHP : Array Functions Tutorial – Part 1
- Function : Check if array index is set and whether it has a value.
- Function: Get Random Element from Array
- What is an Array? Functions of Array- Array Tutorials
- How to Get the Thumbnail of the First Image from the Post and Display it in WordPress
- 11 Most Important String Functions in PHP a programmer should know
- User and Search Engine friendly dynamic URL with PHP and Apache
- Open Source Invoicing Software ? BambooInvoice
- What are class, methods, objects and instance in PHP – Object Oriented Concepts
Related Posts
3 Comments + Add Comment
Leave a comment
Recommended
Related Posts
- PHP : Array Functions Tutorial – Part 3
- PHP : Array Functions Tutorial – Part 1
- Function : Check if array index is set and whether it has a value.
- Function: Get Random Element from Array
- What is an Array? Functions of Array- Array Tutorials
- How to Get the Thumbnail of the First Image from the Post and Display it in WordPress
- 11 Most Important String Functions in PHP a programmer should know
- User and Search Engine friendly dynamic URL with PHP and Apache
- Open Source Invoicing Software ? BambooInvoice
- What are class, methods, objects and instance in PHP – Object Oriented Concepts

An article by





[...] more: PHP : Array Functions Tutorial – Part 2 | My PHP Scripts | A Hub … Share and [...]
[...] Continue reading here: PHP : Array Functions Tutorial – Pаrt 2 | Mу PHP Scripts | A Hub … [...]
Access Control North East…
[...]PHP : Array Functions Tutorial – Part 2 | My PHP Scripts | A Hub for PHP Scripts, PHP tutorial, PHP codes and lots more[...]…