Chapter III using Arrays (2)

Source: Internet
Author: User

    • Multidimensional arrays

   Two-dimensional arrays: equivalent to 1 matrices, or a grid with width and height or rows and columns. For example: This figure holds Bob's product in a two-dimensional array, each line represents a product,

Each column represents the product's attributes (Products attribute).

PHP Code implementation:

//create a two-dimensional array$products 2=Array(Array(' TIR ', ' Tires ', 100),Array(' oil ', ' oil ', 10),Array(' SPK ', ' Spark plugs ', 4));//Show this array in the browserEcho' | '.$products 2[0] [0]. ' | '.$products 2[0] [1]. ' | '.$products 2[0] [2]. " <br/> ";Echo' | '.$products 2[1] [0]. ' | '.$products 2[1] [1]. ' | '.$products 2[1] [2]. " <br/> ";Echo' | '.$products 2[2] [0]. ' | '.$products 2[2] [1]. ' | '.$products 2[2] [2]. " <br/> ";

Example effect:

You can also use a double for loop to achieve the same effect:

 for ($row = 0; $row < 3; $row+ +)    {for ($column = 0;  $column < 3; $column+ +)        {echo "|". $products 2 [$row] [$column];    }     Echo "<br/>";}

         Another implementation of the PHP code (creating a column name instead of a number)-(descriptive index):

//create a two-dimensional array$products 3=Array(Array(' Code ' = ' TIR ', ' Description ' = ' Tires ', ' Price ' =>100),Array(' Code ' = ' oil ', ' Description ' = ' oil ', ' price ' =>10),Array(' Code ' = ' SPK ', ' Description ' = ' Spark plugs ', ' price ' =>4));//Show this array in the browser for($row= 0;$row< 3;$row++){    Echo"|".$products 3[$row[' Code ']. "|".$products 3[$row[' Description ']. "|".$products 3[$row[' Price ']. " <br/> ";}

The example shows the same effect as the previous example.

You can also use the each () and list () functions to traverse the entire inner array in a while loop:

 // show this array in the browser  Span style= "color: #0000ff;" >for  ( $row  = 0; $row  < 3;  $row  ++) {  (list  ( $key ,  $value ) =each  ( $products 3  [ Span style= "color: #800080;" > $row   echo  "| $value  ";  echo  "<br/>" ;}  

If Bob wants to classify his product: Car,van, Truck, use a three-dimensional array:

//create a three-dimensional array$categories=Array(Array(Array(' Car_tir ', ' Tires ', 100),Array(' Car_oil ', ' oil ', 10),Array(' CAR_SPK ', ' Spark plugs ', 4)                            ),Array(Array(' Van_tir ', ' Tires ', 100),Array(' Van_oil ', ' oil ', 10),Array(' VAN_SPK ', ' Spark plugs ', 4)                            ),Array(Array(' Trk_tir ', ' Tires ', 100),Array(' Trk_oil ', ' oil ', 10),Array(' TRK_SPK ', ' Spark plugs ', 4)                        )                    );//Show this array in the browser for($layer= 0;$layer< 3;$layer++){    EchoThe Layer$layer<br/> ";  for($row= 0;$row< 3;$row++){         for($column= 0;$column< 3;$column++){            Echo"|".$categories[$layer][$row][$column]; }        Echo"<br/>"; }}

And so on, you can create four-dimensional, five-D, or six-dimensional arrays.

    • Array sorting

    Use the sort () function:

$products 4 Array (' Tires ', ' oil ', ' Spark plugs '); Sort ($products 4);

Now the product order becomes: oil, Spark plugs, Tires

Note: the sort () function is case-sensitive and all uppercase letters precede lowercase letters, for example:

$products 4 Array (' Tires ', ' oil ', ' Spark plugs '); Sort ($products 4);  for ($row = 0; $row < 3; $row+ +)    {echo$products 4[$row]. " ";}

The results shown are:

    Use the Asort () function and the Ksort () function to sort the associative array: If you store individual items and their prices with associative arrays, you need to use different sort functions to keep the keywords and values consistent when sorting.

The following code creates an array of 3 products and prices, and then sorts them in ascending order of prices:

$products 5 Array (' Tires ' =>100, ' oil ' =>10, ' Spark plugs ' =>4); Asort ($products 5); foreach ($products 5as$key$val) {    echo '$key $val<br/> ";}

Note that the asort is to be traversed with foreach, with the result:

Use Ksort to sort:

$products 6 Array (' Tires ' =>100, ' oil ' =>10, ' Spark plugs ' =>4); Ksort ($products 6); foreach ($products 6as$key$val) {    echo '$key $val<br/> ";}

The example results are:

In addition, the sort ()/asort ()/ksort () each corresponds to a reverse sort function: Rsort (), Arsort (), Krsort ().

Chapter III using Arrays (2)

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: info-contact@alibabacloud.com 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.