Maison >développement back-end >tutoriel php >php »ñÈ¡Êý×éÖµ Êý×éµßµ¹ ¼üÃû²Ù×÷ʵÀý_PHP教程
ÏÂÃæ¾ÙÁ˼¸¸öʵÀýÖ÷ÒªÊǽ²¹ØÓÚÊä³öÊý×éËùÓеÄÖµ array_values ,Ñ»·Êä³öÊý×éµÄ¼ü/Öµ¶Ô²¢Òƶ¯Ö¸Õë reset,½«Êý×éµßµ¹£¬²»±£Áô¼üÃûarray_reverseº¯Êý²Ù×÷¡£
ÏÂÃæ¾ÙÁ˼¸¸öʵÀýÖ÷ÒªÊǽ²¹ØÓÚÊä³öÊý×éËùÓеÄÖµ array_values ,Ñ»·Êä³öÊý×éµÄ¼ü/Öµ¶Ô²¢Òƶ¯Ö¸Õë reset,½«Êý×éµßµ¹£¬²»±£Áô¼üÃûarray_reverseº¯Êý²Ù×÷¡£
*/
$array=array("size"=>"xl","color"=>"gold"); //¶¨ÒåÒ»¸öÊý×é
print_r(array_values($array)); //Êä³öÊý×éËùÓеÄÖµ
/*
¶¨ÒåºÍÓ÷¨
array_values() º¯Êý·µ»ØÒ»¸ö°üº¬¸ø¶¨Êý×éÖÐËùÓмüÖµµÄÊý×飬µ«²»±£Áô¼üÃû¡£
Óï·¨
array_values(array)
*/
$fruit=array('a'=>'apple','b'=>'banana','c'=>'cranberry'); //¶¨ÒåÒ»¸öÊý×é
reset($fruit); //Ö¸ÕëÒƶ¯µÚÒ»¸öλÖÃ
while(list($key,$val)=each($fruit)) //Ñ»·Êä³öÊý×éµÄ¼ü/Öµ¶Ô²¢Òƶ¯Ö¸Õë
{
echo "$key=>$valn"; //Êä³öÊý×éµÄ¼ü¼°Öµ
}
/*
*/
$input=array("php½Ì³Ì",3.0,array("green","red")); //¶¨ÒåÒ»¸öÊý×é
$result=array_reverse($input); //½«Êý×éµßµ¹£¬²»±£Áô¼üÃû
$result_keyed=array_reverse($input,true); //½«Êý×éµßµ¹£¬±£Áô¼üÃû
print_r($result); //ÏÔʾµÚÒ»ÖÖ½á¹û
print_r($result_keyed); //ÏÔʾµÚ¶þÖÖ½á¹û
/*
¶¨ÒåºÍÓ÷¨
array_reverse() º¯Êý½«ÔÊý×éÖеÄÔªËØ˳Ðò·×ª£¬´´½¨ÐµÄÊý×é²¢·µ»Ø¡£Èç¹ûµÚ¶þ¸ö²ÎÊýÖ¸¶¨Îª true£¬ÔòÔªËصļüÃû±£³Ö²»±ä£¬·ñÔò¼üÃû½«¶ªÊ§¡£
Óï·¨
array_reverse(array,preserve)
*/
$array=array(0=>100,"color"=>"red"); //¶¨ÒåÒ»¸öÊý×é
print_r(array_keys($array)); //·µ»Ø¸ÃÊý×éµÄËùÓмüÃû
$array=array("blue","red","green","blue","blue"); //¶¨ÒåÒ»¸öÊý×é
print_r(array_keys($array,"blue")); //·µ»ØֵΪblueµÄ¼üÃû
//¶¨ÒåÒ»¸öÔªËØΪÊý×éµÄÊý×é
$array=array("color"=>array("blue","red","green"),"size"=>array("small","medium","large"));
print_r(array_keys($array)); //·µ»ØÊý×éµÄËùÓмüÃû
/*
¶¨ÒåºÍÓ÷¨
array_keys() º¯Êý·µ»Ø°üº¬Êý×éÖÐËùÓмüÃûµÄÒ»¸öÐÂÊý×é¡£
Èç¹ûÌṩÁ˵ڶþ¸ö²ÎÊý£¬ÔòÖ»·µ»Ø¼üֵΪ¸ÃÖµµÄ¼üÃû¡£
Èç¹û strict ²ÎÊýÖ¸¶¨Îª true£¬Ôò php »áʹÓÃÈ«µÈ±È½Ï (===) À´Ñϸñ¼ì²é¼üÖµµÄÊý¾ÝÀàÐÍ¡£
Óï·¨
array_keys(array,value)