Chapter III using arrays (3)

Source: Internet
Author: User
Tags shuffle

    • Ordering of multidimensional arrays  

user-defined ordering: Like the following code, we know the meaning of the contents of each array, so there are at least two useful sorting methods. We can sort the description of the product alphabetically, or we can sort the price, we need to use

The function usort () tells PHP how to compare individual elements.

//create a two-dimensional array$products 7=Array(Array(' TIR ', ' Tires ', 100),Array(' oil ', ' oil ', 10),Array(' SPK ', ' Spark plugs ', 4));//compile a function to compare X and yfunctionCompare$x,$y){    if($x[1]==$y[1]){        return0; }Else if($x[1]<$y[1]){        return-1; }Else{        return1; }}//Use function compare to sort the arrayUsort($products 7, ' compare ');

Explain: We use keyword function to define a function, we need to give the name of the function, here the name is "compare", the Compare () function with two parameters "$x", "$y", here, $x, $y will be the main array of two sub-arrays, represent one product, respectively.

Since the count is starting at 0, the Description field is the second element of the array, so in order to access the Description field of the array $x (Tires,oil,spark plugs), you need to enter $x[1] and $y[1] to compare the two description fields of the array passed to the function.

The "U" in Usort () stands for "user", because this function requires the passing of a defined comparison function (' compare ').

The above code does not produce any output, we still use a double for loop to output this array:

// Show this array in the browser  for ($row = 0; $row < 3; $row+ +)    {for ($column = 0;  $column < 3; $column+ +)        {echo "".  $products 7[$row] [$column]    ;    } Echo "<br/>";}

The results shown are:

We notice that this is sorted by the size of the letter o,s,t.

reverse user sort: function sort (), Asort (), Ksort () each corresponds to a reverse sort function with the letter "R". But the user-defined sort does not have a reverse variant, but we can write a function that can return the opposite worth of comparisons, for example:

 function  reverse_compare (," Span style= "color: #800080;" > $y   if  ( $x  [1]== $y  [1 return  0;  else  if  (  $x  [1]> $y  [1 return  -1;  else  { return  1; }}
    • Reordering an array

Using the Shuffle () function: We still use the example of the Bob Car store--bob want to make the product on the homepage of the website reflect the characteristic of the company. (He has a lot of products, but would like to randomly select 3 products displayed on the first page, in order to

Don't get bored with multiple login visitors, he wants to see 3 of the products in each visit differently)

The program code is given below: Program listing 3-1 Bob ' s_front_page.php--using PHP to create a dynamic home for Bob's Auto parts store

<?PHP$pictures=Array(' tire.jpg ', ' oil.jpg ', ' spark_plug.jpg ', ' door.jpg ', ' steering_wheel.jpg ', ' thermostat.jpg ', ' wiper_blade.jpg ', ' Gasket.jpg ', ' brake_pad.jpg ');Shuffle($pictures);?>s Auto parts</title>' s Auto partsPHP for($i= 0;$i< 3;$i++){                    Echo"<td align=\" Center\ ">; Echo $pictures[$i]; Echo"\"/></td> "; }                ?> </tr> </table> </div></body>

This is the file directory:

Effect Show:

    Use the Array_reserve () function: Use an array as a parameter to return an array with the same contents as the parameter array but in reverse order. For example, there are many ways to create an array that contains numbers 10 through 1 in reverse order.

The first: Use the range () function alone to create an ascending array, and then use the Rsort () function or the Array_reserve () function to change the numbers in the array to descending order;

The second is to use a For loop to create this array in one element at a time,

$numbers Array ();  for ($i = 10; $i > 0; $i--) {    $numbers$i;} Print_r ($numbers);

Example results:

Note: You can use the Array_push () function, like this:

$stack Array ("Orange", "banana"); Array_push ($stack, "apple", "raspberry"); Print_r ($stack);

Example results:



Chapter III using arrays (3)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.