Home > Backend Development > PHP Tutorial > Common tricks for PHP arrays_PHP tutorial

Common tricks for PHP arrays_PHP tutorial

WBOY
Release: 2016-07-21 14:52:08
Original
930 people have browsed it

//Read array
$arr=array("xx"=>"Chacha","yy"=>"Waiwai");
echo "Read the first one:".$arr[xx];//Count the number of arrays
$array=array(aaa,bbb,ccc,ddd);
echo "The number of arrays is:".count($array)."n"; //Calculate the number of array units or the number of attributes in the object. Same as sizeof()//Quickly create an array
$arr_range = range(0,20,2); //Define the range of values, minimum 0, maximum 6, step size is 2 (difference, optional)
$arr_range2 = range("A","N");
echo "Use range() to quickly create a numeric array:";
print_r($arr_range);
echo "n";
echo "Use range() to quickly create an array of letters:";
print_r($arr_range2);
echo "n";//Test array
if(is_array($arr_range)){ echo "$arr_range is an array!n"; }else{echo "is not an array!n";}//Split the array
$str="Pig, kitten, puppy";
list($a,$b,$c) =explode(",",$str);
echo "The array is split into: ".$a ." ". $b ." ". $c;
echo "
";//Add elements to the head of the array
$oldarr=array("Xiao Zhang", "Xiao Li", "Xiao Wang", "Xiao Zhao");
$newarr=array_unshift($oldarr,"Xiao Sun","Xiao Zhou","Xiao Wu");
print_r($oldarr); //This array variable is at the head and a new
is added echo "The number is:".print_r($newarr);//Add elements at the end of the array
$oldarr=array("Xiao Zhang", "Xiao Li", "Xiao Wang", "Xiao Zhao");
$newarr=array_push($oldarr,"Xiao Sun","Xiao Zhou","Xiao Wu");
print_r($oldarr); //This array variable is added to the end with a new
echo "The number is:".print_r($newarr);//Delete the value from the array header
$oldarr=array("Xiao Zhang", "Xiao Li", "Xiao Wang", "Xiao Zhao");
$newarr=array_shift($oldarr);
print_r($newarr); //Return the deleted value
print_r($oldarr); //Return the remaining value after being deleted //Delete the value from the end of the array
$oldarr=array("Xiao Zhang", "Xiao Li", "Xiao Wang", "Xiao Zhao");
$newarr=array_pop($oldarr);
print_r($newarr); //Return the deleted value
print_r($oldarr); //Return the remaining deleted //positioned elements: search array
$search="Xiao Liu";
$oldarr =array("Xiao Zhang", "Xiao Li", "Xiao Wang", "Xiao Zhao", "Xiao Liu");
if(in_array($search,$oldarr)){ echo "There is: ".$search."n" in the array;}else{echo "There is no such thing in the array: ".$search."n";}//Location element :Search associative array "key"
$oldarr["Fruit1"]="Watermelon";
$oldarr["Fruit 2"]="Peach";
$oldarr["Fruit 3"]="Grapes";
if(array_key_exists("Fruit 2",$oldarr)){ printf("The fruit you selected is: %s n",$oldarr["Fruit 2"]);} else {echo "This does not exist!";} //Locate element: search associative array "value"
$oldarr["Fruit1"]="Watermelon";
$oldarr["Fruit 2"]="Peach";
$oldarr["Fruit 3"]="Grapes";
$found=array_search("Watermelon",$oldarr);
if($found){ printf("The value of %s is: %s n",$found,$oldarr[$found]);} else {echo "This does not exist!";}//Get " in the array key"
$oldarr=array("Strength"=>"Boxing","Speed"=>"Running");
while($key = key($oldarr)){
printf("%s
",$key);
next($oldarr); //Each time the key will not move the pointer, next is needed to move the pointer
}//Get the "value" in the array
$oldarr=array("Strength"=>"Boxing","Speed"=>"Running");
while($val = current($oldarr)){
printf("%s
",$val);
next($oldarr); //Each time the key will not move the pointer, next is needed to move the pointer backwards prev() moves forward by one reset() moves the first position end() moves the end
}//Traverse the "keys and values" in the array
$arr=array("one", "two", "three");
foreach ($arr as $value){
echo "Value: " . $value . "
";
}//Count the frequency of occurrence of array elements (the result is the number of values)
$arr=array("one", "two", "three", "two", "three");
print_r(array_count_values($arr));//Determine the unique array element (output non-duplicate content)
$arr=array("one", "two", "three", "two", "three");
print_r(array_unique($arr));//Reverse the order of array elements (keys and values)
$arr=array("one", "two", "three", "four", "five");
print_r(array_reverse($arr));//Replace array and value
$arr=array("one", "two", "three", "four", "five");
print_r(array_flip($arr));//Positive array sorting (key and value association is no longer maintained)
$arr=array("one", "two", "three", "four", "five");
sort($arr);
print_r($arr);//Positive array sorting (key and value association maintained)
$arr=array("one", "two", "three", "four", "five");
asort($arr);
print_r($arr);//Reverse array sorting (key and value association is not maintained)
$arr=array("one", "two", "three", "four", "five");
rsort($arr);
print_r($arr);//Reverse array sorting (key and value association maintained)
$arr=array("one", "two", "three", "four", "five");
arsort($arr);
print_r($arr);//Naturally sorted array (classic sort)
$arr=array("a1", "a3", "a10", "a22", "a19");
natsort($arr);
print_r($arr);//Naturally sorted array (classic sorting, case-insensitive)
$arr=array("A1", "a3", "a10", "A22", "a19");
natcasesort($arr);
print_r($arr);//Sort the array by key value
//ksort();
//Sort array keys in reverse order
//krsort();
//Sort according to user-defined rules
$arr=array('2009-02-06','2009-02-10','2009-02-13','10-06-2009','9-17-2009');
sort($arr);
print_r($arr);//Merge, split, join, and decompose arrays (recursively merge one or more arrays, pay attention to the quotes of the keys)
$arr_a = array("John"=>100,"James"=>85);
$arr_b = array("Micky"=>78,"John"=>45);
$newarr = array_merge_recursive($arr_a,$arr_b);
print_r($newarr);//Recursively append array
$arr_a = array("AK"=>"xx","AV","AO","AE"); //Use the key of the first array
$arr_b = array("Akssjd"=>"xasd","Aosdwe","Aesadlkj","Avxiwqlk"); //Use the value of the second array
$newarr = array_combine($arr_a,$arr_b);
print_r($newarr);//Link two arrays (keys automatically sequence numbers, values ​​merge, keys are displayed separately, keys with the same value will be overwritten)
$arr_a = array("W",a=>"O","F","R","C");
$arr_b = array("2","6","4","7","3",a=>"x","0","9");
$newarr = array_merge($arr_a,$arr_b);
shuffle($newarr); //Shuffle (shuffle the array), the result is different every time
print_r($newarr);//Split the array
$oldarr=array("Watermelon","Apple","Grapefruit","Banana","Mango","Kiwi","Hawthorn");
$newarr=array_slice($oldarr,2,-2); //Remove the first two and the last two
print_r($newarr); //New array
print_r($oldarr); //All original arrays//Join the arrays (form two new arrays)
$oldarr=array("Watermelon","Apple","Grapefruit","Banana","Mango","Kiwi","Hawthorn");
$newarr=array_splice($oldarr,2);
print_r($newarr); //New array
print_r($oldarr); //Deleted array//Find the communication of the array
$new_a=array("A","B","C","D","E","F"); //Using the "key and value" first row array as the standard, 3 arrays "value" "has exactly the same output
$new_b=array("B","X","E","R","W","P");
$new_c=array("Z","N","B","W","E","C");
$newarr = array_intersect($new_a,$new_b,$new_c);
print_r($newarr);//Find the intersection of associative arrays (similar to the one above)
//array_intersect_assoc() This considers that the "keys" are the same. If the "keys" and "values" are the same, the output result is
//Find the difference set of arrays (this is exactly the opposite of intersect())
$new_a=array("A","B","C","D","E","F"); //Using the "key and value" first row array as the standard, 3 arrays "value" "There is no output at all
$new_b=array("B","X","E","R","W","P");
$new_c=array("Z","N","B","W","E","C");
$newarr = array_diff($new_a,$new_b,$new_c);
print_r($newarr);//Find the intersection of associative arrays (similar to the one above)
//array_diff_assoc() This considers the "key" to be the same, if there is no output result for both "key" and "value"
//Return a set of random "keys"
$oldarr = array("aaa"=>"dasd","bbb"=>"axsae","ccc"=>"fdvdf","ddd"=>"ewmcc");
$newarr = array_rand($oldarr,2);
print_r($newarr);//Shuffle the array randomly and randomly disrupt the order of "values"
$oldarr = array(a=>"sd","sc","xe","po","ed");
shuffle($oldarr);
print_r($oldarr);//Sum the values ​​in the array
$oldarr = array(36,"hello",75);
$newarr = array_sum($oldarr);
print $newarr;//Decompose the array into a multi-dimensional array
$oldarr=array("A","B","C","D","E","F","G","H","I","J","K"," L","M","N","O","P","Q","R"); //shuffle($oldarr); //Shuffle the cards
$newarr=array_chunk($oldarr,4);
print_r($newarr);
/*Find the number that does not exist in the array */
$arr =array(2,3,5,7,9,11,13,14,16,23,26,28,29,31,32,33,35,37,39,41,46,49);
for($i=0;$i $randnum = rand(0,50);
if(in_array($randnum,$arr)){
break;
}else{
printf("Does not exist %d
",$randnum);
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/371687.htmlTechArticle//Read array $arr=array(xx=Chacha,yy=Waiwai); echo read out The first one: .$arr[xx];//Count the number of arrays $array=array(aaa,bbb,ccc,ddd); echo The number of arrays is: .count($array).n;//Calculate the array Single...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template