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.

 

There are some basic PHP Array Questions and Answers for interviews.

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
 
3) How to create an array in PHP?
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 5
 
5) What is the difference between array_combine and array_merge
array_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.

6) What is array_combine:
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!";
  }
?>
 
10) How to create an array from a PHP string?
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?
 
There are basically three types of arrays available in PHP. They are:

  1. Indexed arrays
  2. Associative arrays
  3. 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

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP