array functions in php Introduction. PHP array() function creates and returns an array.
array functions in php – PHP Array Functions
array functions in php – there are the list of the Commonly used PHP5 Array Functions Like as array_slice($arr, $offset, $length), array_rand($arr), array_reverse($arr), array_flip($arr), array_map(‘function_name’, $arr), sort($arr), array_shift($arr), array_push($arr, $val), array_pop($arr), array_keys($arr), array_values($arr), array_merge($arr1, $arr2), print_r($arr), in_array($var, $arr), is_array($arr) and sizeof($arr).
sizeof($arr)
Example
<?php $lists_of_olympic_medalists = array("Uday", "Hitesh", "Avantika"); echo "Size of the array is: ". sizeof($lists_of_olympic_medalists); ?>
Results
Size of the array is: 3
is_array($arr)
Example
<?php $lists_of_olympic_medalists = array("Uday", "Hitesh", "Avantika"); // using ternary operator echo is_array($lists_of_olympic_medalists) ? 'Array' : 'not an Array'; $mymember = "Uday"; // using ternary operator echo is_array($mymember) ? 'Array' : 'not an Array'; ?>
Results
Array
not an Array
in_array($var, $arr)
Example
<?php $lists_of_olympic_medalists = array("Uday", "Hitesh", "Avantika"); $concept = "Ella"; echo in_array($concept, $lists_of_olympic_medalists) ? 'Added to the Lineup' : 'Not yet!' ?>
Results
Not yet!
Don’t Miss : PHP associative array push
print_r($arr)
Example
<?php $lists_of_olympic_medalists = array("Uday", "Hitesh", "Avantika"); print_r($lists_of_olympic_medalists); ?>
Results
Array (
[0] => “Uday”
[1] => “Hitesh”
[2] => “Avantika”
)
array_merge($arr1, $arr2)
Example
<?php $exam_pass_list = array( "Krishna" => "India", "Bhumika" => "Fukky", "Devraj" => "Swarg", "Movie" => "Tamil" ); $members = array("Vishal", "Jayshree", "Naveent", "Sanjay"); $merged = array_merge($exam_pass_list, $members); print_r($merged); ?>
Results
Array ( [Krishna] => India [Bhumika] => Fukky [Devraj] => Swarg [Movie] => Tamil [0] => Vishal [1] => Jayshree [2] => Naveent [3] => Sanjay )
Don’t Miss : PHP check if value exists in associative array
array_values($arr)
Example
<?php $exam_pass_list = array( "Krishna" => "India", "Bhumika" => "Fukky", "Devraj" => "Swarg", "Movie" => "Tamil" ); // members who own the above members $members = array("Vishal", "Jayshree", "Naveent", "Sanjay"); // let's merge the two arrays into one $merged = array_merge($exam_pass_list, $members); //getting only the values $merged = array_values($merged); print_r($merged); ?>
Results
Array ( [0] => India [1] => Fukky [2] => Swarg [3] => Tamil [4] => Vishal [5] => Jayshree [6] => Naveent [7] => Sanjay )
array_keys($arr)
Example
<?php //getting only the keys $keys = array_values($merged); print_r($keys); ?>
Results
Array ( [0] => Krishna [1] => Bhumika [2] => Devraj [3] => Movie [4] => 0 [5] => 1 [6] => 2 [7] => 3 )
array_pop($arr)
Example
<?php $lists_of_olympic_medalists = array("Uday", "Hitesh", "Avantika"); // removing the last element array_pop($lists_of_olympic_medalists); print_r($lists_of_olympic_medalists); ?>
Results
Array ( [0] => Uday [1] => Hitesh )
array_push($arr, $val)
Example
<?php $lists_of_olympic_medalists = array("Uday", "Hitesh", "Avantika"); // adding a new element at the end array_push($lists_of_olympic_medalists, "Ella"); print_r($lists_of_olympic_medalists); ?>
Results
Array ( [0] => Uday [1] => Hitesh [2] => Avantika [3] => Ella )
Don’t Miss : Php filter multidimensional array by multiple values
array_shift($arr)
Example
<?php $lists_of_olympic_medalists = array("Uday", "Hitesh", "Avantika"); // removing the first element array_shift($lists_of_olympic_medalists); print_r($lists_of_olympic_medalists); ?>
Results
Array ( [0] => Hitesh [1] => Avantika )
sort($arr)
Example
<?php $lists_of_olympic_medalists = array("Uday", "Hitesh", "Avantika", "Ella"); // sort the array sort($lists_of_olympic_medalists); print_r($liverankingmembers); ?>
Results
Array ( [0] => Avantika [1] => Ella [2] => Hitesh [3] => Uday )
array_map(‘function_name’, $arr)
Example
<?php function addOne($val) { // adding 1 to input value return ($val + 1); } $numbers = array(10, 20, 30, 40, 50); $numbers = array_map('addOne', $numbers); print_r($numbers) ?>
Results
Array ( [0] => 11 [1] => 21 [2] => 31 [3] => 41 [4] => 51 )
array_flip($arr)
Example
<?php $exam_pass_list = array( "Krishna" => "India", "Bhumika" => "Fukky", "Devraj" => "Swarg", "Movie" => "Tamil" ); print_r(array_flip($exam_pass_list)); ?>
Results
Array ( [India] => Krishna [Fukky] => Bhumika [Swarg] => Devraj [Tamil] => Movie )
array_reverse($arr)
Example
<?php $ranks = array(10, 20, 30, 40, 50); print_r(array_reverse($ranks)); ?>
Results
Array ( [0] => 50 [1] => 40 [2] => 30 [3] => 20 [4] => 10 )
Don’t Miss : PHP array functions
array_rand($arr)
Example
<?php $members = array("rani", "bheem", "bhumi", "grishna", "waqdur", "yatin"); echo "member of the day: ". $members[array_rand($members)]; ?>
Results
member of the day: grishna
array_slice($arr, $offset, $length)
Example
<?php $members = array("rani", "bheem", "bhumi", "grishna", "waqdur", "yatin"); print_r(array_slice($members, 2, 3)); ?>
Results
Array ( [0] => bhumi [1] => grishna [2] => waqdur )
Don’t Miss : PHP array functions with examples
I hope you get an idea about array functions in php.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.
I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.