<?php //1.initiate an array //1 $a = array(1, 2, 3); //2 $b = [4, 5, 6]; //3 $numbers = range(1, 10); //1,2,3,4,5,6,7,8,9,10 $odds = range(1, 10, 2); //1,3,5,7,9 $odds = range(10, 1, -1); //9,7,5,3,1 $letters = range('a', 'z'); //a-z echo '<pre>'; //var_dump($numbers); //var_dump($odds); //var_dump($letters); $a[3] = 4; //automatically resize the array //4 $student = ['amy', 'joseph', 'mike']; //read array for ($i = 0; $i < 3; $i++) { // echo $student[$i]; } foreach ($student as $name) { //echo $name.'foreach'; } //5 $grades = array('amy' => 89, 'joseph' => 99, 'mike' => 100); //6 $grades = array('amy' => 89); $grades['joseph'] = 99; $grades['mike'] = 100; //7 $grades['amy'] = 89; $grades['joseph'] = 99; $grades['mike'] = 100; //2.using loops foreach ($grades as $student => $grade) { //echo $student.'='.$grade; } while ($element = each($grades)) { //var_dump($element); } reset($grades); //because each() tracks every elements to next one, // if you want to use the array again you need to reset it while (list($stu, $grade) = each($grades)) { //echo $stu.'='.$grade; } //3.sort $student = ['zamy', 'joseph', 'mike']; sort($student);//sort by ASCII //print_r($student); $file = array(file01, file06, file02); sort($file, SORT_NATURAL);//sort by ASCII //print_r($file); $product = array('oil' => 100, 'tyre' => 58, 'plugs' => 45); asort($product);//sort by value //print_r($product); ksort($product); //sort by key //print_r($product); //ascending above descending corespondingly are rsort(), arsort(), krsort(); $products = array( array('eTLR', 'aTires', 100), //code, desc, price,x array('gOIL', 'bOil', 10), array('fSPK', 'cSpark Plugs', 4) ); ///for multi-mension array array_multisort($products); //sort by the first element //print_r($product); //4.usort user-defined sort function compare($x, $y) { // x is a kind of product if ($x[0] == $y[0]) { //0 stands for eTLR,,,, 1 stands for aTires, 2 stands for 100 return 0; } elseif ($x[0] > $y[0]) { //x > y return 1 then ascending, vice versas, if x<y, return 1, descending return 1; } else { return -1; } } usort($products, 'compare'); //compare is a "callable" ,also change the array, this function is from multi-mension arrays //similarly there also other functions such as: uasort(), uksort(), print_r($products); shuffle($product); $reversedArray = array_reverse($product); // there is no &, so this function does not change the product array, instead it returns a reverse array $numbers = array(); for ($i = 10; $i > 0; $i--) { array_push($numbers, $i); // add the end element of the nmubers } //print_r($numbers); array_pop($numbers); //remove the end element from the numbers //print_r($numbers); $str = '123d3ll3'; echo intval($str); //123 //5.array pointer : every array has a internal pointer that pointed the current element $arr = array(1, 2, 3, 4, 5); echo current($arr); //1 echo each($arr); //return array(1=>2) but pointer points to the next one 2 echo next($arr); // 3 echo end($arr); //5 echo prev($arr); //4 echo '<br/>'; //6. array_walk($arr,callable func[, mixed userData]) //applying any function to each element in a array. using array_walk($arr,callable func[, mixed userData]) // callable func is the name of a user-defined function that applies the same change to every element. function myPrint($value) { echo "$value<br/>"; } //this will not change the arr due to &,using for loop array_walk($arr, "myPrint"); //myPrint is the name of the function, print_r($arr); echo "<hr>"; function myMultiply($v, $key, $factor){ //there is a sequence of $value and $key, // the first one is the value and second is the key. $key is not used although declared here $v *= $factor; echo "$v<br/>"; } //this will not change the arr due to & in the first parameter function myMultiply(&$v, $k, $factor) { //&$v means that $v will be passed by reference $v *= $factor; echo "$v<br/>"; } array_walk($arr, "myMultiply", 5); // print_r($arr); echo count($arr); //5 echo sizeof($arr); //5 //7.array_count_values(); //caculate the frequency of the element in a array $arrF = array(1, 3, 4, 3, 3, 2, 4, 2, 1); $ac = array_count_values($arrF); print_r($ac); //k => v, 1 =>2 means 1 digit occurs 2 times in the arrF echo "<hr>"; //8.extract(); $array = array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'); extract($array); echo $key1, $key2, $key3; //value1value2value3 //$key1,$key2,$key3 variables already exist $arr2 = array('key1' => 'valueA', 'keyB' => 'valueB', 'keyC' => 'valueC'); extract($arr2); //extract($arr2),EXTR_OVERWRITE); by default echo $key1, $keyB, $keyC; //valueAvalueBvalueC extract($array, EXTR_PREFIX_ALL, 'myPrefix'); //link variable with _ echo $myPrefix_key1, $myPrefix_key2, $myPrefix_key3; //value1value2value3
来源:https://www.cnblogs.com/luoxuw/p/12231124.html