Php Array functions interview Questions And Answers with example

1)PHP array_column() Function?
The array_column() function returns the values from a single column in the input array.

array_column(array,column_key,index_key);

<?php
// An array that represents a possible record set returned from a database
$a = array(
array(
‘id’ => 5698,
‘first_name’ => ‘Peter’,
‘last_name’ => ‘Griffin’,
),
array(
‘id’ => 4767,
‘first_name’ => ‘Ben’,
‘last_name’ => ‘Smith’,
),
array(
‘id’ => 3809,
‘first_name’ => ‘Joe’,
‘last_name’ => ‘Doe’,
)
);

$last_names = array_column($a, ‘last_name’, ‘id’);
print_r($last_names);
?>
Output:

Array
(
[5698] => Griffin
[4767] => Smith
[3809] => Doe
)

2)Combine 2 Arrays of Different Lengths in php?

function array_combine2($arr1, $arr2) {
$count = min(count($arr1), count($arr2));
return array_combine(array_slice($arr1, 0, $count), array_slice($arr2, 0, $count));
}

OR

$res = array_combine(array_intersect_key($arr1, $arr2), array_intersect_key($arr2, $arr1));

3) array_chunk() Function in php?

The array_chunk() function splits an array into chunks of new arrays.

Syntax

array_chunk(array,size,preserve_key);

 

Parameter Description
array Required. Specifies the array to use
size Required. An integer that specifies the size of each chunk
preserve_key Optional. Possible values:

  • true – Preserves the keys
  • false – Default. Reindexes the chunk numerically

Technical Details

Return Value: Returns a multidimensional indexed array, starting with zero, with each dimension containing size elements
PHP Version: 4.2+

Examples

Split an array into chunks of two and preserve the original keys:

<?php
$fruits=array(“Apple”,”Banana”,”Mango”,”Graps”,”Orange”,”Painepal”);
print_r(array_chunk($fruits,2));
?>
Output
Array ( [0] => Array ( [0] => Apple [1] => Banana )
[1] => Array ( [0] => Mango [1] => Graps )
[2] => Array ( [0] => Orange [1] => Painepal ) )

4)PHP array_combine() Function?

Definition and Usage:

The array_combine() function creates an array by using the elements from one “keys” array and one “values” array.

READ  Check if value and key exist in multidimensional array

Note: Both arrays must have equal number of elements!

Syntax:
array_combine(keys,values);

you can see below there Parameters and Descriptions:

keys:(Required) Array of keys

values:(Required) Array of values

<?php
$fname=array(“Niks”,”Nikunj”,”Raj”);
$age=array(“26″,”30″,”35”);
$c=array_combine($fname,$age);
print_r($c);
?>

Out Put
Array ( [Niks] => 26 [Nikunj] => 30 [Raj] => 35 )

5)PHP array_count_values() Function ?

array_count_values() Function used for Count all the values of an array:

Example:

<?php
$a=array(“A”,”Cat”,”Dog”,”A”,”Dog”);
print_r(array_count_values($a));
?>

Out Put
Array ( [A] => 2 [Cat] => 1 [Dog] => 2 )
Definition and Usage:

The array_count_values() function counts all the values of an array.

Syntax:

array_count_values(array)
you can see below there Parameters and Descriptions:

array: (Required) Specifying the array to count values of

6)PHP array_diff() Function?

The array_diff() function compares the values of two (or extra) arrays, and returns the variations.

This function compares the values of two (or extra) arrays, and return an array that contains the entries from array1 that aren’t present in array2 or array3, etc.

Syntax:

array_diff(array1,array2,array3…);
you can see below there Parameters and Descriptions:

array1: (Required) The array to compare from

array2: (Required) An array to compare against

array3,… (Optional) More arrays to compare against
Example:
<?php
$a1=array(“a”=>”red”,”b”=>”green”,”c”=>”blue”,”d”=>”yellow”,”e”=>”orange”);
$a2=array(“f”=>”red”,”g”=>”green”,”h”=>”blue”,”i”=>”yellow”);

$result=array_diff($a1,$a2);
print_r($result);
?>

Out Put
Array ( [e] => orange)

7)PHP array_diff_key() Function?

The array_diff() function compares the values of two (or extra) arrays, and returns the variations.
This function compares the values of two (or extra) arrays, and return an array that contains the entries from array1 that aren’t present in array2 or array3, etc.

Syntax:

array_diff(array1,array2,array3…);
you can see below there Parameters and Descriptions:

READ  PHP Interview questions and answers

array1: (Required) The array to compare from

array2: (Required) An array to compare against

array3,… (Optional) More arrays to compare against

<?php
$a1=array(“a”=>”red”,”b”=>”green”,”c”=>”blue”);
$a2=array(“a”=>”red”,”c”=>”blue”,”d”=>”pink”);

$result=array_diff_key($a1,$a2);
print_r($result);
?>
Out Put
Array ( [b] => green )

 

8)PHP array_diff_assoc() Function?

The array_diff_assoc() function compares the keys and values of two (or extra) arrays, and returns the variations.

This function compares the keys and values of two (or extra) arrays, and return an array that contains the entries from array1 that aren’t present in array2 or array3, etc.

Syntax:
array_diff_assoc(<em>array1,array2,array3…);
you can see below there Parameters and Descriptions:

array1: (Required) The array to compare from
array2: (Required) An array to compare against
array3,…: (Optional) More arrays to compare against

Example:

<?php
$a1=array(“a”=>”red”,”b”=>”green”,”c”=>”blue”,”d”=>”yellow”,”e”=>”orange”);
$a2=array(“f”=>”red”,”g”=>”green”,”h”=>”blue”,”i”=>”yellow”);

$result=array_diff_assoc($a1,$a2);
print_r($result);
?>
Out Put
Array ( [e] => orange)

Leave a Reply

Your email address will not be published. Required fields are marked *