欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

php »ñÈ¡Êý×éÖµ Êý×éµßµ¹ ¼üÃû²Ù×÷ʵÀý

程序员文章站 2022-05-02 22:38:05
...
ÏÂÃæ¾ÙÁ˼¸¸öʵÀýÖ÷ÒªÊǽ²¹ØÓÚÊä³öÊý×éËùÓеÄÖµ 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)