There is more than fifty array functions available in PHP. You can check the list of PHP array functions. Here i list top five most popular PHP array functions.
To Five Most Popular Array Functions
1. is_array() function
Syntax-
is_array($argument)
It returns true if the argument is an array otherwise returns false.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$argument = array('value1','value2'); if(is_array($argument)){ echo 'array'; } else { echo 'Not an array'; } // Output - array |
2. in_array() function
syntax-
in_array($search_value,$array,mode);
mode is optional part in in_array function. By default it’s false, if it’s true then it’s check for type also.
In array function check the search value in an array. Returns true if it present otherwise false.
1 2 3 4 5 6 7 |
$array_val = array('12', '5', '6', '4'); if(in_array(4,$array_val)){ echo 'value found in an array'; } |
3. array_merge() function
array_merge() function merge one or more arrays.
1 2 3 |
syntax- array_merge($array1,$array2); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$first_array = array('value1','value2'); $second_array = array('vikash','raj'); $array_merge_result = array_merge($first_array,$second_array); print_r($array_merge_result); Output- Array ( [0] => value1 [1] => value2 [2] => vikash [3] => raj ) |
list() function
list() function is very useful when you want to assign an array items to variables.
How to Return Multiple Values in PHP
1 2 3 4 5 6 7 8 9 10 11 |
$value = array('php','javascript','python'); list($first_value,$second_value,$third_value) = $value; // when you echo the individual variables echo $first_value; // output - php echo $second_value; // output - javascript echo $third_value; // output - python |
count() function
Count() function counts the value in an array or an object.
1 2 3 4 5 |
$array_val = array('lang','val','two'); echo count($array_val); //output - 3 |