PHP Array function is still more commonly used, so I studied the PHP array function, here to take out and share with you, hope to be useful to everyone. Today we learn PHP array function, array is a type of data, it is very high frequency, learn to deal with the array will make your site handy.
For example: Your site database has a Dali article, now you want to display 20 entertainment news on a page title, the 20 headings from the database after you want to use 20 variables to represent it? I think you are not so stupid, so what variables can fully receive the 20 data? This will use the array variable. What is an array? Array is actually a data collection, equivalent to a data container, a lot of data stored in the inside, we can be stored in or out of a certain way, but also the data inside it can be sorted and other operations, but also to check if there is no data we want, and so on.
1.array_multisort () Sorting multiple arrays or multidimensional arrays can be used to sort multiple arrays at once or to sort multidimensional arrays based on a dimension. Sorting preserves the original key-name associations, and implements the same functionality as in SQL.
- $ ar1 = Array ("Ten", 100,100, "a");
- $ ar2 = Array (1, 3, "2", 1);
- Array_multisort ($ar 1, $ar 2);
- ?>
- Returns the result (preserving the association of the original array)
- $ ar1 = Array ([0]=>10[1]=>a[2]=>100[3]= > + )
- $ ar2 = Array ([0]=>1[1]=>1[2]=>2[3]= > 3)
- Achieving student Performance rankings
- $ Grade = Array ("Score" =>Array (70,95,70.0,60, "" "),
- "Name" = > Array ("Zhangsan", "LiSi", "Wangwu" ,
- "Zhaoliu", "Liuqi"));
- Array_multisort ($grade ["Score"],sort_numeric,sort_desc,
- Sort fractions as numbers, from high to low
- $grade ["name"],SORT_STRING,SORT_ASC);
- Sort names as strings, from small to large
- ?>
- Output results
- Array
- ' score ' = >
- Array
- 0 = > ,
- 1 = > ' A ',
- 2 = > ,
- 3 = > ,
- 4 = > ,
- ),
- ' name ' = >
- Array
- 0 = > ' LiSi ',
- 1 = > ' Liuqi ',
- 2 = > ' Wangwu ',
- 3 = > ' Zhangsan ',
- 4 = > ' Zhaoliu ',
- ),
- )
2.array_rand () and Shuffle (), one is to take one or more values randomly from the array, the other is to scramble the order of the values in the array, and then remove the values from them, all of which implement the function of randomly extracting data from the array, which can be used to implement functions such as sweepstakes.
3.array_reduce () iterates the callback function functions into each cell in the input array, simplifying the array to a single value. If an optional parameter, initial, is specified, the parameter is treated as the first value in the array or, if the array is empty, as the final return value.
- php
- Functionrsum ($v, $w) {
- $v + = $w;
- Return$v;
- }
- Functionrmul ($v, $w) {
- $v *= $w;
- Return$v;
- }
- $ a = Array (1,2,3,4,5);
- $ x = Array ();
- Implementing the Array_sum () function
- $ b = Array_reduce ($a, "rsum");// 1 the =1+2+3+4+5
- $ C = Array_reduce ($a, "rmul");// 1 - =1*2*3*4*5*10
- $ D = Array_reduce ($x, "Rsum", 1);//1
- ?>
This function can be implemented to find an array of and (product, poor function)
http://www.bkjia.com/PHPjc/446540.html www.bkjia.com true http://www.bkjia.com/PHPjc/446540.html techarticle PHP Array function is still more commonly used, so I studied the PHP array function, here to take out and share with you, hope to be useful to everyone. Today we are learning PHP array functions, ...