Practice-Questions-on-PHP-arrays
Bit Questions on PHP Arrays
Here is the list of top PHP array practice questions that can be asked by the Interviewer in PHP Interviews to Freshers and Experience.
1) PHP array means what?
PHP array is a variable that stores multiple values in a single variable.
2) How many types of arrays in PHP?
3 Types
An array can be created in two ways in PHP. One is using array() function or by using [] in front of variable.
$myarray = array(); or $myarray =[];
4) How to get the array size in PHP?
You can use the array_size() function to get the size of the number of the PHP Array.
$cities =['kolkata','delhi','mumbai','banglore','chennai'];
echo count($cities);
// outputs 5array_combine() is used to create a new array by using the key of one array as keys and the value of another array as values. Whereas
array_merge() merges one or more than one array such that the value of one array appended at the end of the first array.
The array_combine() function creates an array by using the elements from one "keys" array and one "values" array.
<!DOCTYPE html>
<html>
<body>
<?php
$countries = array("USA","UK","France","Australia");
$capitals=array("NewYork","London","Paris", "Canberra");
$c=array_combine($countries,$capitals);
print_r($c);
?>
</body>
</html>
7) What is array_merge:
The array_merge() function creates an array by appending an array to another array.
9) How to find a key that exists in an array?
PHP array_key_exists() Function is used to check if a key exists or not in an array.
Example Uses
<?php
$car = array("color"=>"red","brand"=>"BMW" "model" = > "2020");
if (array_key_exists("color",$car))
{
echo "Array Key exists!";
}
else
{
echo "Array Key does not exist!";
}
?>
The PHP explode() function is used to create an array from a string in PHP. Explode function breaks a string using a delimiter into an array.
Below is the basic syntax to convert a string to an array in PHP
<?php
$string = "This is a string in PHP";
$array= explode(" ",$string);
print_r(" ",$array);
?>
12) How to Delete an element from an array?
You can use array_splice() or unset() function to delete an element from a PHP array.
13) What is array_splice() ?
delete an element from a PHP array ie., removes selected elements from an array and replaces it with new elements and also returns an array with the removed elements.
syntax:
<?php
$input = array("red", "green", "blue", "yellow");
print_r(array_splice($input, 3)); // Array ( [0] => yellow )
print_r($input); //Array ( [0] => red [1] => green [2] => blue )
?>
14) List types of arrays are available in PHP?
- Indexed arrays
- Associative arrays
- Multidimensional arrays
15) List functions available to sort a PHP array?
PHP functions for Sorting Arrays are
sort(): sorts arrays in ascending order.
rsort(): sorts arrays in descending order
asort(): sorts associative arrays in ascending order, by value
ksort(): sorts associative arrays in ascending order, by key
arsort(): sorts associative arrays in descending order, by value
krsort(): sorts associative arrays in descending order, by key
16) How to convert a JSON string to an array in PHP?
json_dcode() function is used to convert a JSON string to an array in PHP.
$jsonobj = '{"apple":35,"orange":37,"tomato":43}';
var_dump(json_decode($jsonobj));
17) How to encode an associative array into a JSON object:
$items = array("apple" => 15, "orange" => 20, "tomato" => 40);
echo json_encode($items);
{"apple":15,"orange":20,"tomato":40}
18) Difference between indexed and associative array in PHP.
Indexed arrays, elements identified by their position. Associative arrays
each element associated with some key, keys are unique regardless of
whether the key is a string or an integer.
Comments
Post a Comment