|
|
 |
array_keys (PHP 4, PHP 5) array_keys -- Return all the keys of an array Descriptionarray array_keys ( array input [, mixed search_value [, bool strict]] )
array_keys() returns the keys, numeric and
string, from the input array.
If the optional search_value is specified,
then only the keys for that value are returned. Otherwise, all
the keys from the input are returned.
As of PHP 5, you can use strict parameter for
comparison including type (===).
Example 1. array_keys() example |
<?php
$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));
$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));
$array = array("color" => array("blue", "red", "green"),
"size" => array("small", "medium", "large"));
print_r(array_keys($array));
?>
|
The above example will output: Array
(
[0] => 0
[1] => color
)
Array
(
[0] => 0
[1] => 3
[2] => 4
)
Array
(
[0] => color
[1] => size
) |
|
See also array_values() and
array_key_exists().
User Contributed Notes
array_keys
devmnky /at\ gmail /dot\ com
09-Mar-2005 08:03
Please note that array_keys() does not work if you're trying to search for values within multi-dimensional arrays.
For example:
$array = array("color" => array("blue", "red", "green"),
"size" => array("small", "medium", "large"));
print_r(array_keys($array, "blue"));
will return:
Array
{
}
steve at ukwebsystems dot com
25-Sep-2004 12:52
note to sip at email dot ee
inefficent it may be, but it detects if array keys have been defined with null values.
with the array
$Row['field1'] = null;
$Row['field2'] = 'hello';
array_key_exists('field1',$Row) will return true
isset($Row['field1']) will return false, even though the key is present...
sip at email dot ee
22-Aug-2003 07:33
Note, that using array_key_exists() is rather inefficient. The overhead associated with calling a function makes it slower, than using isset($array[$key]), instead of array_key_exists($key, $array)
using isset() is usually about 1.3 times faster, according to my tests.
rodrigo at NOSPAM dot dhweb dot com dot br
04-Feb-2003 06:39
[Editor's note: For a complete solution to the printing of complex structures or hashes, see the PEAR::Var_Dump package: http://pear.php.net/package-info.php?pacid=103 , use "pear install Var_Dump" to get it]
This function will print all the keys of a multidimensional array in html tables.
It will help to debug when you donīt have control of depths.
<?php
function show_keys($ar){
echo "<table width='100%' border='1' bordercolor='#6699CC' cellspacing='0' cellpadding='5'><tr valign='top'>";
foreach ($ar as $k => $v ) {
echo "<td align='center' bgcolor='#EEEEEE'>
<table border='2' cellpadding='3'><tr><td bgcolor='#FFFFFF'><font face='verdana' size='1'>
" . $k . "
</font></td></tr></table>";
if (is_array($ar[$k])) {
show_keys ($ar[$k]);
}
echo "</td>";
}
echo "</tr></table>";
}
$arvore = array();
$arvore['1'] = array();
$arvore['1']['1.1'] = array('1.1.1', '1.1.2', '1.1.3');
$arvore['1']['1.2'] = array('1.2.1', '1.2.2', '1.2.3');
$arvore['1']['1.3'] = array('1.3.1', '1.3.2', '1.3.3');
$arvore['2'] = array();
$arvore['2']['2.1'] = array('2.1.1', '2.1.2', '2.1.3');
$arvore['2']['2.2'] = array('2.2.1', '2.2.2', '2.2.3');
$arvore['2']['2.3'] = array('2.3.1', '2.3.2', '2.3.3');
$arvore['3'] = array();
$arvore['3']['3.1'] = array('3.1.1', '3.1.2', '3.1.3');
$arvore['3']['3.2'] = array('3.2.1', '3.2.2', '3.2.3');
$arvore['3']['3.3'] = array('3.3.1', '3.3.2'=>array('3.3.2.1', '3.3.2.2'), '3.3.3');
show_keys($arvore);
?>
michielbakker at msn dot com
13-Nov-2002 11:45
If you receive a bunch of variables and like to change most of them (or all of them for that matter), you can do something like this: (data has been sent to a page with POST)
<?
$allKeys = array_keys($HTTP_POST_VARS);
for ($i=0;$i<count($allKeys);$i++)
{
$$allKeys[$i] = strtoupper($HTTP_POST_VARS[$allKeys[$i]]);
}
?>
This makes caracters (a-z) uppercase. This is just one way to use it, ofcourse.
Hope this helps someone understand the way to use array_keys() or give any ideas. :)
glennh at webadept dot net
13-Nov-2002 06:03
All the cool notes are gone from the site.
Here's an example of how to get all the variables passed to your program using the method on this page. This prints them out so you can see what you are doing.
<?php
while (list($key, $value) = each
(${"HTTP_".$REQUEST_METHOD."_VARS"}))
{
echo $key." = ".$value." ";
}
?>
jacob at keystreams dot com
21-Aug-2002 01:05
Here is a way to use array_intersect on array keys rather than values:
<?php
$a = array("apple" => "red", "banana" => "yellow");
$z = array("apple" => "green", "peach" => "orange", "banana" => "rotten");
$intersected_keys = array_intersect(array_keys($a), array_keys($z));
print_r($intersected_keys);
?>
This will print:
Array ( [0] => apple [1] => banana )
| |