★ wanayoo — archive 1999 http://fr.php.net/manual/cs/function.array-keys.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<array_key_existsarray_map>
view the version of this page
Last updated: Sat, 27 Nov 2004

array_keys

(PHP 4 , PHP 5)

array_keys -- Vrátit všechny klíče pole

Popis

array array_keys ( array input [, mixed search_value])

array_keys() vrací klíče, numerické i textové, z pole input.

Pokud je přítomen volitelný argument search_value, vrací pouze klíče této hodnoty. Jinak vrací všechny klíče z pole input.

Příklad 1. Ukázka array_keys()

$array = array (0 => 100, "color" => "red");
array_keys ($array);      // vrací array (0, "color")

$array = array ("blue", "red", "green", "blue", "blue");
array_keys ($array, "blue");  //  vrací array (0, 3, 4)

$array = array ("color" => array("blue", "red", "green"), "size" => array("small", "medium", "large"));
array_keys ($array);  //  vrací array ("color", "size")

Poznámka: Tato funkce byla přidána v PHP 4, dále je uvedena implementace pro ty, kteří stále používají PHP 3.

Příklad 2. Implementace array_keys() pro uživatele PHP 3

function array_keys ($arr, $term="") {
   $t = array();
   while (list($k,$v) = each($arr)) {
       if ($term && $v != $term)
           continue;
           $t[] = $k;
       }
       return $t;
}

Viz také: array_values().



add a note add a note User Contributed Notes
array_keys
steve at ukwebsystems dot com
25-Sep-2004 07: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 02: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
05-Feb-2003 01: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>";

}

// Multidimensional array ->
$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 06: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 01: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 08: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 )

<array_key_existsarray_map>
 Last updated: Sat, 27 Nov 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2004 The PHP Group
All rights reserved.
This mirror generously provided by: nexen.net
Last updated: Tue Nov 30 05:17:37 2004 CET