Wednesday, 24 September 2014

array_keys in PHP

array_keys() function is used to get all the keys of an input array.
PHP Array_keys function gives a single array and generates a new array with new key values for keys of the input array and new keys are numeric starting with zero.

Syntax: array_keys(array,value)

Parameters Description:
array : Required. Assign the array.
value : Optional. Assigning a value returns keys with value.

strict : Optional. Used with the value parameter.
Possible values :
  • true - Returns the keys with the assigned value, It depending on type: the number 5 is not the same as the string "5".
  • false - Default value. Not depending on type, the number 5 is the same as the string "5".

Example for PHP array_keys()

<?php
$daysarr1 = array("a"=>"Sunday","b"=>"Monday","c"=>"Tuesday");
    echo "without using value parameter"."<br />";
print_r(array_keys($daysarr1));
$daysarr2 = array("a"=>"Sunday","b"=>"Monday","c"=>"Tuesday");
echo "using value parameter"."<br />";
print_r(array_keys($daysarr2,"Monday"));
$daysarr3 = array(10,20,30,"10");
echo "using the strict parameter : false"."<br />";
print_r(array_keys($daysarr3,"10",false));
$daysarr4 = array(10,20,30,"10");
echo "using the strict parameter : true"."<br />";
print_r(array_keys($daysarr3,"10",true));
?>

Output will be:


without using value parameter
Array (
          [0] => a
          [1] => b
          [2] => c
         )
using value parameter
Array (
          [0] => b
         )
using the strict parameter : false
Array (
          [0] => 0
          [1] => 3 
         )
using the strict parameter : true
Array (
          [0] => 3
         )

0 comments:

Post a Comment