?????個人主頁:@開發(fā)者-曼億點
????? hallo 歡迎 點贊?? 收藏? 留言?? 加關(guān)注?!
????? 本文由 曼億點 原創(chuàng)
????? 收錄于專欄:PHP程序開發(fā)
????
—
?前言?
??(1) 二維數(shù)組
??與一維數(shù)組相比,二維數(shù)組能夠存儲更加豐富的數(shù)據(jù)。相對于一維數(shù)組只有一鍵值維度–列,二維數(shù)組具有兩個鍵值維度–行和列。
如果是索引數(shù)組,則分別表示數(shù)組的行和列。數(shù)組維度圖描述了索引數(shù)組中一維數(shù)和二維數(shù)組的維度,如果是關(guān)聯(lián)數(shù)組,則只需要將行及列中的數(shù)字改為鍵名和值即可。
??數(shù)組維度圖:
??二維數(shù)組的本質(zhì)是二維數(shù)組中存放的元素都是一維數(shù)組,創(chuàng)建二維數(shù)組的語法格式如下:
//創(chuàng)建二維數(shù)組,存放的元素為數(shù)組$arr =array(arrl,arr2,..,arrN);
??二維數(shù)組僅僅通過列鍵值獲取數(shù)組元素:array[col],而二維數(shù)組多了一個鍵值維度因此,獲取數(shù)組元素需要行鍵值和列鍵值:array[rowl[col]。
??創(chuàng)建二維數(shù)組:
<?php
//創(chuàng)建數(shù)組存放索引數(shù)組
$arr1 =array(array(0,0,100),array(1, 1));
print_r($arr1);
echo "<br />";
echo"第1行,第3列的值 arr1[0][2]為:". $arr1[0][2];
echo "<br />".
//創(chuàng)建數(shù)組存放關(guān)聯(lián)數(shù)組
$arr2 = array(
array ("name"=>"商品 A","price"=>"100","img"=>"images/1.jpg"),
array("name"=>"商品 B","price"=>"200","img"=>"images/2.jpg"));
print_r($arr1);
echo "<br />";
echo"第2行,列鍵值為name 的值為arr2[1][name]為:".$arr2[1]["name"];
?>
??如圖為結(jié)果圖:
??(2) 二維數(shù)組遍歷
??二維數(shù)組多了一個行維度,因此需要兩個循環(huán)遍歷,第一個循環(huán)負責行,第二個循環(huán)負責列。由于 for循環(huán)是以次數(shù)作為循環(huán)條件的,因此 for 循環(huán)適合索引數(shù)組。
??創(chuàng)建二維數(shù)組:
<?php
//創(chuàng)建數(shù)組存放索引數(shù)組
$arr1 = array(array(0,0,100),array(1,1),array(2,3,4,5));
print_r($arr1);
echo "<br />";
//計算數(shù)組行數(shù)
$row_num = count($arr1);
//for 循環(huán)遍歷行
for($row=0;$row< $row_num; $row++){
//取出數(shù)組,計算列數(shù)
$col_num = count($arr1[$row]);
//for 循環(huán)遍歷列
for($co1=0;$col < $col_num; $col++){
echo $arr1[$row][$col]." ";
}
echo "<br />";
}
//創(chuàng)建數(shù)組存放關(guān)聯(lián)數(shù)組
$arr2 = array(
array("name"=>"商品 A","price"=>"100”","img"=>"images/1.jpg"),
array("name"=>"商品 B","price"=>"200","img"=>"images/2.jpg")
);
print_r($arr2);
echo "<br />";
//遍歷行
foreach ($arr2 as $key=>$value){
//獲取一維數(shù)組后,直接通過鍵值獲取".
echo $value['name'].",".$value['price'].",".$value['img' ]."<br />";
}
?>
??運行結(jié)果如圖:
??(3)二維數(shù)組的排列
?? 維數(shù)組中可以使用sort、asort和ksort 等函數(shù)進行排序,二維數(shù)組沒有直接用于排序的函數(shù),需要自己實現(xiàn)。例如,排序后的商品統(tǒng)計表,要對里面的商品按價格從高到低的規(guī)則排序,排序后商品統(tǒng)計表見排序后的商品統(tǒng)計表。
??排序后的商品統(tǒng)計表
商品名稱 | 價格/元 | 圖片地址 |
---|---|---|
商品A | 89 | /images/1.jpg |
商品B | 88 | images/2.jpg |
商品C | 100 | images/3.jpg |
排序后的商品統(tǒng)計表
商品名稱 | 價格/元 | 圖片地址 |
---|---|---|
商品c | 100 | /images/3.jpg |
商品A | 89 | images/1.jpg |
商品B | 89 | images/2.jpg |
??在二維數(shù)組中對數(shù)組進行排序,可以利用 array_column函數(shù)和 array_multisort 函數(shù)
??anray_multisort 函數(shù)返回一個排序數(shù)組,語法格式如下:
array_multisort(arrl,sorting,order,sorting type, arr2, arr3,....arrN);
??參數(shù)order可選,默認值為SORT_ASC,表示按升序排列。也可以指定SORTDESC 按降序排列。參數(shù)type可選,規(guī)定排序類型,默認為SORT_REGULAR,表示按常規(guī)順序排列。參數(shù) arr1必選,arr1l中的元素值為排序的依據(jù),一般為一維數(shù)組,參數(shù)arr2,arr3,…,arrN可選。anray_multisont 數(shù)先對第一個數(shù)組元素值進行排序,接著是后面的數(shù)組,且后面數(shù)組的順序按照對應(yīng)的第一個數(shù)組的值排列。相當于將排序的多個數(shù)組以列為單位,按第一個數(shù)組元素值進行排序。
??(4) 商品的顯示,商品的排序和商品的查找
??新建商品的靜態(tài)頁面:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>商品管理</title>
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<link rel="stylesheet" href="css/weui.css"/>
<!-- 自定義的css -->
<link rel="stylesheet" href="css/demo.css"/>
</head>
<body ontouchstart>
<!-- 將頂部固定 -->
<div class="search_top">
<!-- 搜索框組件 -->
<div class="weui-search-bar">
<form class="weui-search-bar__form" method="GET" action="index.php">
<div class="weui-search-bar__box">
<input type="search" class="weui-search-bar__input" name="key"
style="padding-left:10px" placeholder="輸入查找的商品" value=""/>
<button class="weui-icon-search" type="submit"></button>
</div>
</form>
</div>
<!-- 操作按鈕 -->
<div class="btn-area">
<a class="weui-btn weui-btn_mini" href="index.php?sort=asc">價格升序</a>
<a class="weui-btn weui-btn_mini" href="index.php?sort=desc">價格降序</a>
<a class="weui-btn weui-btn_mini" href="index.php">顯示全部</a>
</div>
</div>
<!-- 由于頂部導(dǎo)航固定,將商品顯示距離頂部100px,防止給搜索遮擋 -->
<div class="goods-container">
<div class="weui-flex">
<!-- 第一列 -->
<div class="weui-flex__item left">
<div class="goods">
<img src="images/1.jpg" alt="">
<p class="title">華為筆記本MateBook</p>
<p class="price">5600</p>
</div>
</div>
<!-- 第二列
<div class="weui-flex__item right">
<div class="goods">
<img src="images/2.jpg" alt="">
<p class="title">華為屏幕MateView</p>
<p class="price">2600</p>
</div>
</div> -->
</div>
</div>
</body>
</html>
商品頁面樣式設(shè)置:
/*設(shè)置頁面背景顏色為灰色*/
body {
background-color: #ededed
}
/*設(shè)置頂部搜索組件和操作按鈕固定*/
.search_top{
position:fixed;
width:100%;
z-index: 1;
}
/*設(shè)置操作按鈕背景顏色與頁面背景顏色一致*/
.btn-area{
background-color: #ededed;
}
.weui-btn {
font-size: 13px;
color: black;
font-weight: normal;
}
/*設(shè)置商品距離頂部80px,防止頂部固定部分遮擋商品*/
.goods-container {
padding-top:80px;
background-color: #ededed;
}
/*左右列間距,注意左列右邊距和右列左邊距都是5px*/
.left{
margin: 10px 5px 10px 10px;
}
.right{
margin: 10px 10px 10px 5px;
}
/*設(shè)置列里面商品底部間距,圖片圓角,背景顏色白色*/
.goods {
margin-bottom:5px;
border-radius: 10px;/*與img圓角配合使用*/
background-color: white;
text-align: center;
}
/*設(shè)置圖片寬度100%,圖片圓角*/
.goods img {
border-radius: 10px;
width: 100%;
height: 100%;
}
.title {
padding-top: 10px;
}
.price {
color: red;
}
效果圖:
??首頁面由兩部分構(gòu)成,分別是置項的組件(包含搜索框和機作被圍和晁示商品容器組件。搜索框中使用了表單提交關(guān)鍵字,表單使用 POST 方式提交數(shù)據(jù)當前頁面。
商品管理的總代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>商品管理</title>
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<link rel="stylesheet" href="css/weui.css"/>
<!-- 自定義的css -->
<link rel="stylesheet" href="css/demo.css"/>
</head>
<body ontouchstart>
<?php
//定義一個二維數(shù)組,存儲商品
$goods=array(
array("img"=>"images/1.jpg","name"=>"華為筆記本","price"=>"10000"),
array("img"=>"images/2.jpg","name"=>"華為手表","price"=>"2000"),
array("img"=>"images/3.jpg","name"=>"華為路由器","price"=>"3080"),
array("img"=>"images/4.jpg","name"=>"華為電視劇","price"=>"4090"),
array("img"=>"images/5.jpg","name"=>"華為手機","price"=>"52223")
);
//去接收排序的規(guī)則
if(isset($_GET['sort']))
{
//定義排序規(guī)則
$price=array_column($goods,'price');//獲取商品數(shù)組的價格列
//獲取升降序
$sort_type=$_GET['sort'];
if($sort_type=='asc'){
//為升序
$sort=SORT_ASC;
}else
{
//為降序
$sort=SORT_DESC;
}
//調(diào)用排序的方法
array_multisort($price,$sort,$goods);
}
//接收查詢的內(nèi)容
if(isset($_GET['key'])){
$key_value=$_GET['key'];//查找的商品
$search=array();
foreach($goods as $key=>$value)
{
$index=array_search($key_value,$value);
if($index!==false)
{
$goods=array();
$goods[]= $value;
}
}
}
$goods_left=array();
$goods_right=array();
for($i=0;$i<count($goods);$i++)
{
if($i%2==0){
array_push($goods_left,$goods[$i]);//存放顯示在左邊的數(shù)據(jù)
}else{
array_push($goods_right,$goods[$i]);//存放顯示在右邊的數(shù)據(jù)
}
}
?>
<!-- 將頂部固定 -->
<div class="search_top">
<!-- 搜索框組件 -->
<div class="weui-search-bar">
<form class="weui-search-bar__form" method="GET" action="test.php">
<div class="weui-search-bar__box">
<input type="search" class="weui-search-bar__input" name="key"
style="padding-left:10px" placeholder="輸入查找的商品" value=""/>
<button class="weui-icon-search" type="submit"></button>
</div>
</form>
</div>
<!-- 操作按鈕 -->
<div class="btn-area">
<a class="weui-btn weui-btn_mini" href="test.php?sort=asc">價格升序</a>
<a class="weui-btn weui-btn_mini" href="test.php?sort=desc">價格降序</a>
<a class="weui-btn weui-btn_mini" href="test.php">顯示全部</a>
</div>
</div>
<!-- 由于頂部導(dǎo)航固定,將商品顯示距離頂部100px,防止給搜索遮擋 -->
<div class="goods-container">
<div class="weui-flex">
<div class="weui-flex__item left">
<?php
//創(chuàng)建二維數(shù)組
//遍歷左的商品組,并顯示在界面上
foreach($goods_left as $key=>$value)
{
?>
<div class="goods">
<img src=<?php echo $value['img']?> alt="">
<p class="title"><?php echo $value['name']?></p>
<p class="price"><?php echo $value['price']?></p>
</div>
<?php
}
?>
</div>
<div class="weui-flex__item right">
<?php
//創(chuàng)建二維數(shù)組
//遍歷左的商品組,并顯示在界面上
foreach($goods_right as $key=>$value)
{
?>
<div class="goods">
<img src=<?php echo $value['img']?> alt="">
<p class="title"><?php echo $value['name']?></p>
<p class="price"><?php echo $value['price']?></p>
</div>
<?php
}
?>
</div>
</div>
</div>
</body>
</html>
效果圖如下:
提示:按自己的圖片路徑匹配文章來源:http://www.zghlxwxcb.cn/news/detail-845852.html
結(jié)束語??
以上就是PHP程序設(shè)計
持續(xù)更新PHP程序設(shè)計教程,歡迎大家訂閱系列專欄??PHP程序開發(fā)你們的支持就是曼億點創(chuàng)作的動力??????文章來源地址http://www.zghlxwxcb.cn/news/detail-845852.html
到了這里,關(guān)于【項目實戰(zhàn)】——商品管理的制作完整代碼的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!