In the previous chapters of this tutorial, we already know that an array is a simple list of number/value pairs.
However, there are times when you want to store values with more than one key.
Can be stored in multidimensional arrays.
PHP-Multidimensional arrays
A multidimensional array refers to an array that contains one or more arrays.
PHP can understand multi-dimensional arrays of two or three, four, or five levels or more. However, arrays that are more than three levels deep are difficult for most people to manage.
Note: The dimensions of an array indicate the number of indexes you need to select an element.
- For a two-dimensional array, you need two indexes to select the element
- For three-dimensional arrays, you need three indexes to select the elements
PHP-two-D arrays
A two-d array is an array of arrays (three-dimensional arrays are arrays of arrays).
First, let's take a look at the following table:
| Brand |
Stock |
Sales |
| Volvo |
33 |
20 |
| BMW |
17 |
15 |
| Saab |
5 |
2 |
| Land Rover |
15 |
11 |
We can store the data in the table in a two-D array, like this:
<! DOCTYPE html><?php$cars = array ( array("Volvo",33,20), array("BMW",17,15), array("Saab",5,2), array("Land Rover",15,11) ); echo $cars[0][0].": 库存:".$cars[0][1].", 已售:".$cars[0][2].".<br>";echo $cars[1][0].": 库存:".$cars[1][1].", 已售:".$cars[1][2].".<br>";echo $cars[2][0].": 库存:".$cars[2][1].", 已售:".$cars[2][2].".<br>";echo $cars[3][0].": 库存:".$cars[3][1].", 已售:".$cars[3][2].".<br>";?> </body>
Output
Volvo: In Stock: 33, Sold: 20.
BMW: Stock: 17, sold: 15.
Saab: Stock: 5, Sold: 2.
Land Rover: Stock: 15, sold: 11.
<! DOCTYPE html><?php$cars = array ( array("Volvo",33,20), array("BMW",17,15), array("Saab",5,2), array("Land Rover",15,11) ); for ($row = 0; $row < 4; $row++) { echo "<p><b>行数 $row</b></p>"; echo "<ul>"; for ($col = 0; $col < 3; $col++) { echo "<li>".$cars[$row][$col]."</li>"; } echo "</ul>";}?> </body>
PHP Date and time
The PHP date () function is used to format dates or times.
PHP Date () function
The PHP date () function formats the timestamp as an easier-to-read date and time.
Grammar
Date (Format,timestamp)
| Parameters |
Description |
| Format |
Necessary. Specifies the format of the timestamp. |
| Timestamp |
Optional. Time stamp is specified. The default is the current time and date. |
Get a simple date
The format parameters of the date () function are required, and they specify how the date or time is formatted.
Some of the characters commonly used in dates are listed below:
- D-Represents the day of the month (01-31)
- M-Denotes month (01-12)
- Y-Represents the year (four digits)
- 1-Indicates the day of the week
Other characters, such as "/", "." or "-" can also be inserted into characters to add additional formatting.
The following example formats today's date in three different ways:
Instance
<?phpecho "Today is". Date ("Y/m/d"). "<br>"; echo "Today is." Date ("Y.M.D"). "<br>"; echo "Today is." Date ("y-m-d"). "<br>"; echo "Today is." Date ("L");? >
Today is 2016/05/05
Today is 2016.05.05
Today is 2016-05-05
Today is Thursday.
PHP Tips-Automatic copyright year
Use the date () function to automatically update the version year on your site:
Instance
©2010-<?php echo Date ("Y")?>
Output All rights reserved 2008-2016
<! DOCTYPE html><?phpecho "当前时间是 " . date("h:i:sa");?> </body>
Current time is 05:00:46pm
Note: Be aware that the PHP date () function returns the current date/time of the server!
Get time Zone
If the correct time is returned from the code, it is possible that your server is in another country or is set to a different time zone.
Therefore, if you need accurate time based on the exact location, you can set the time zone to use.
The following example sets the time zone to "Asia/shanghai" and then outputs the current time in the specified format:
Instance
<?phpdate_default_timezone_set ("Asia/shanghai"); echo "Current time is". Date ("H:i:sa");? >
The result of the output is that the current time is 05:09:04pm
Create a date with PHP mktime ()
The optional timestamp parameter in the date () function specifies the time stamp. If you do not specify a timestamp, the current date and time will be used (as in the previous example).
The Mktime () function returns the Unix timestamp of the date. The UNIX timestamp contains the number of seconds between the Unix era (January 1, 1970 00:00:00 GMT) and the specified time.
Grammar
Mktime (Hour,minute,second,month,day,year)
The following example uses a series of parameters in the Mktime () function to create a date and time:
Instance
<?php$d=mktime (9, A, 6, ten); echo "Creation date is". Date ("Y-m-d H:i:sa", $d);? >
The creation date is 2015-06-10 09:12:31am
Create a date with a string using PHP strtotime ()
The PHP strtotime () function is used to convert human-readable strings to Unix time.
The time of the returned string
Grammar
Strtotime (Time,now)
The following example creates a date and time by using the Strtotime () function:
Instance
<?php$d=strtotime ("10:38pm April"); echo "Creation date is". Date ("Y-m-d H:i:sa", $d);? >
Convert a human string into time
Run results
2016-05-06 12:00:00am
2016-05-07 12:00:00am
2016-08-05 05:34:30pm
More Date Instances
The following example prints the date of the next Saturday:
Instance
<?php$startdate = Strtotime ("Saturday"), $enddate = Strtotime ("+6 weeks", $startdate); while ($startdate < $enddate { echo date ("M D", $startdate), "<br>"; $startdate = Strtotime ("+1 Week", $startdate);}? >
The following example outputs the number of days before July 4:
Instance
<?php$d1=strtotime ("December"), $d 2=ceil (($d 1-time ())/60/60/24), echo "from December 31 also:". $d 2. "Days. ";? >
The following example output from December 31 is also: 240 days.
PHP multi-dimensional array Advanced Chapter