Array Functions in PHP


Array Functions in PHP

Array Functions:

1. array_change_key_case(): Changes all keys in an array to lowercase or uppercase

Syntax:

array_change_key_case(array,case);

array Required. Specifies the array to use

case Optional. Possible values:

CASE_LOWER - Default value. Changes the keys to lowercase

CASE_UPPER - Changes the keys to uppercase

Example:

Change all keys in an array to uppercase:

<?php

$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");

print_r(array_change_key_case($age,CASE_UPPER));

?>

Output: Array ( [PETER] => 35 [BEN] => 37 [JOE] => 43 )

1. array_combine():Create an array by using the elements from one "keys" array and one "values" array:

Syntax:

array_combine(keys,values);

Example:

<?php

$fname=array("Peter","Ben","Joe");

$age=array("35","37","43");

$c=array_combine($fname,$age);

print_r($c);

?>

Output:   Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )

array_count_values():

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

Syntax:

array_count_values(array)

Example:

Count all the values of an array:

<?php

$a=array("A","Cat","Dog","A","Dog");

print_r(array_count_values($a));

?>

Output:  Array ( [A] => 2 [Cat] => 1 [Dog] => 2 )

array_fill() Function:

 The array_fill() function fills an array with values.

Syntax:

array_fill(index,number,value);

index Required. The first index of the returned array

number Required. Specifies the number of elements to insert

value Required. Specifies the value to use for filling the array

Example:

<?php
$a1=array_fill(3,4,"blue");
$b1=array_fill(0,1,"red");
print_r($a1);
echo "<br>";
print_r($b1);
?>

Output:

Array ( [3] => blue [4] => blue [5] => blue [6] => blue ) 
Array ( [0] => red )

array_replace():

Replace the values of the first array ($a1) with the values from the second array ($a2):

Syntax:

array_replace(array1,array2)

array1 Required. Specifies an array

array2 Optional. Specifies an array which will replace the values of array1

Example:

<?php

$a1=array("red","green");

$a2=array("blue","yellow");

print_r(array_replace($a1,$a2)); ?>

Output:

Array ( [0] => blue [1] => yellow )

6.array_rand():

The array_rand() function returns a random key from an array, or it returns an array of random keys if you specify that the function should return more than one key.

Syntax:

    array_rand(array,number)

     array Required. Specifies an array

        number Optional. Specifies how many random keys to return

Example:

<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
print_r(array_rand($a,2));
?>

Output:  a

7.array_reverse():

The array_reverse() function returns an array in the reverse order.

Syntax:

array_reverse(array)

Example:

<?php

echo "<br> ARRAY REVERSE<br>";

$a=array("a"=>"Volvo","b"=>"BMW","c"=>"Toyota");

print_r(array_reverse($a));

?>

Output:  Array ( [c] => Toyota [b] => BMW [a] => Volvo )

 8.Array_search():

  Searches an array for a given value and returns the key

Syntax:

array_search(value,array,strict)

value -Required. Specifies the value to search for

array -Required. Specifies the array to search in

strict-Optional. If this parameter is set to TRUE, then this function will search for identical elements in the array. Possible values:true

false - Default

Example:

<?php

$a=array("a"=>"red","b"=>"green","c"=>"blue");

echo array_search("red",$a);

?>

Output: b

9. asort():

Sort an associative array in ascending order, according to the value:

Syntax:

 asort(array);

array Required. Specifies the array to sort

The asort() function sorts an associative array in ascending order, according to the value.

Sort an associative array in ascending order, according to the value:

Example:    

 <?php

$age=array("Peter"=>"35","Ben"=>"29","Joe"=>"43");

asort($age);

foreach($age as $x=>$x_value)

  {  echo "Key=" . $x . ", Value=" . $x_value;

  echo "<br>";

  }

?>

output: Key=Ben, Value=29
Key=Peter, Value=35
Key=Joe, Value=43

10.ksort():

Sort an associative array in ascending order, according to the key:

Syntax:

ksort(array,sortingtype);

array Required. Specifies the array to sort

sortingtype Optional. Specifies how to compare the array elements/items. Possible values:

0 = SORT_REGULAR - Default. Compare items normally (don't change types)

1 = SORT_NUMERIC - Compare items numerically

2 = SORT_STRING - Compare items as strings

3 = SORT_LOCALE_STRING - Compare items as strings, based on current locale

4 = SORT_NATURAL - Compare items as strings using natural ordering

5 = SORT_FLAG_CASE –

Example:

<?php

$age=array("Aeter"=>"76","Ben"=>"37","coe"=>"43");

ksort($age);

foreach($age as $x=>$x_value)

  {

  echo "Key=" . $x . ", Value=" . $x_value;

  echo "<br>";

  } ?>

Output:

Key=Aeter, Value=76
Key=Coe, Value=43
Key=Ten, Value=37.

Custom Search

%d bloggers like this: