PHP Array Functions
PHP array functions are regular among the very important ones that interviewers select for candidates. For those who are unaware of PHP array, this is a data structure that allows developers to store multiple elements with similar data type under a single variable, which save developers the effort to create a different variable for every data. The PHP arrays functions are extremely helpful creating a list of elements of similar types, which developers will able to access using their key or index.
Types of arrays in PHP;
- Numeric or Indexed Arrays,
- Multidimensional Arrays
- Associative Arrays
Most Frequently Asked PHP Array Functions
Associative Arrays | Indexed or Numeric Arrays |
---|---|
This is a type of arrays which used named specific keys to assign and store values in the database. | This type of arrays store and assign values in a numeric fashion with count starting from zero. |
Example of an associative array:
$name_two["i am"] = "zara";
$name_two["anthony is"] = "Hello";
$name_two["ram is"] = "Ananya";
echo "Accessing elements directly:\n";
echo $name_two["i am"], "\n";
echo $name_two["anthony is"], "\n";
echo $name_one["Ram is"], "\n";
Output:
Accessing elements directly:
zara
Hello
Ananya
Example of an indexed or numeric array:
$name_two[1] = "Sonu Singh";
echo "Accessing the array elements directly:\n";
echo $name_two[1], "\n";
Output:
Sonu Singh
You can either use the PHP var_dump()
or print_r()
function to check the structure and values of an array. The var_dump()
method gives more information than print_r()
.
$lists = array("Best", "Interview", "Questions");
// Print the array
print_r($lists);
var_dump($lists);
is_array() : It is an inbuilt function used in PHP. It is used to check whether a variable is an array or not.
in_array() : It is used to check whether a given value exists in an array or not. It returns TRUE if the value is exists in array, and returns FALSE otherwise.
$array = array('My','Name','Is','BestInterViewQuestion');
echo implode(" ",$array)
// OUTPUT : My Name Is BestInterViewQuestion
PHP explode() function is used to break a string into an array.
$string = "My Name Is BestInterviewQuestion";
print_r (explode(" ",$string));
// OUTPUT : Array ( [0] => My [1] => Name [2] => Is [3] => BestInterviewQuestion )
array_search() is a inbuilt function of PHP which is used to search a particular value in an array and if the value is found then it returns its corresponding its key.
$array = array("1"=>"My", "2"=>"Name", "3"=>"is", "4"=>"BestInterviewQuestion");
echo array_search("BestInterviewQuestion",$array);
// OUTPUT 4
For this we can use array_reverse() function.
$array = array("1"=>"Best","2"=>"Interview","3"=>"Question");
print_r(array_reverse($array));
// OUTPUT Array ( [3] => Question [2] => Interview[1] => Best)
We can use the PHP array_unique()
function to remove the duplicate vlaues form an array.
$array = array("a"=>"best","b"=>"interviewquestion","c"=>"best");
print_r(array_unique($array));
// OUTPUT : Array ( [a] => best
[b] => interviewquestion
)
Both are used to count elements in a array.sizeof() function is an alias of count() function used in PHP. count() function is faster and butter than sizeof().
$array = array('1','2','3','4');
$size = count($array);
//OUTPUT : 4
We can check a variable with the help of is_array() function in PHP. It's return true if variable is an array and return false otherwise.
$var = array('X','Y','Z');
if (is_array($var))
echo 'It is an array.';
else
echo 'It is not an array.';
Arrays help developers store multiple elements within a single variable that can be accessed using a key or an index. We can also combine it with for each statement to a quick loop through an array with the use of very little code. While web application development, we can store tabular data in a pair of nested arrays to achieve better performance and utility. PHP arrays functions also have various functions such as merge, sort, split and intersect which makes it easy for us to efficiently manipulate them. With the inefficiency of strong typing and OOP-object iteration support in PHP, arrays are the most preferable data structure to store and manipulate data. We will further continue here with top PHP Array interview questions picked by industry leaders with their suggested answers for your practice.