㈠ php中 二维数组更换键值
代码:
$arr=array();
for($i=0;$i<4;$i++){//4可以换成数组的长度
$arr[$i]=>array(
'count'=>$count[$i],//原两个等长的数组
'sku_id'=>$sku_id[$i]
);
}
print_r($arr);//测试
㈡ php 合并多维数组中相同的值
$arr=array(
array(
array(
"stocknotifyId"=>1,
"sku"=>'13809_BL_37',
"email"=>'[email protected]'
),
array(
"stocknotifyId"=>3,
"sku"=>'13809_BL_37',
"email"=>'[email protected]'
),
array(
"stocknotifyId"=>5,
"sku"=>'13809_BL_37',
"email"=>'[email protected]'
)
),
array(
array(
"stocknotifyId"=>2,
"sku"=>'13809_B_39',
"email"=>'[email protected]'
)
),
array(
array(
"stocknotifyId"=>4,
"sku"=>'13809_BL_39',
"email"=>'[email protected]'
)
)
);
$re=array();
foreach($arras$a){
foreach($aas$v){
$tmp_v=$v;
unset($tmp_v['email']);
if(isset($re[$v['email']])){
$re[$v['email']][]=$tmp_v;
}else{
$re[$v['email']]=array($tmp_v);
}
}
}
print_r($re);
<!--output-->
Array
(
[[email protected]]=>Array
(
[0]=>Array
(
[stocknotifyId]=>1
[sku]=>13809_BL_37
)
[1]=>Array
(
[stocknotifyId]=>2
[sku]=>13809_B_39
)
)
[[email protected]]=>Array
(
[0]=>Array
(
[stocknotifyId]=>3
[sku]=>13809_BL_37
)
[1]=>Array
(
[stocknotifyId]=>4
[sku]=>13809_BL_39
)
)
[[email protected]]=>Array
(
[0]=>Array
(
[stocknotifyId]=>5
[sku]=>13809_BL_37
)
)
)
㈢ php mysql_fetch_assoc 循环遍历表格
有2个办法,第一种直接使用sql的多表联查,效率高,但是得到的数据table1会被扩展成table2一样的条目数 要再次处理
select*fromtable1a,table2bwherea.orderid=b.orderid
第二种方法,先得到table11的数据,在循环中匹配table2到一个新的列名中隐敏行
$conn=mysqli_connect("127.0.0.1","root","123","test");
$sql="select*fromtable1";
$rs=mysqli_query($conn,$sql);
$Arr=拿败array();
while($row=mysqli_fetch_assoc($rs)){
$sql="select*fromtable2whereorderid=".$row["orderid"];
$row["order_sku"]=mysqli_fetch_all(mysqli_query($conn,$sql),MYSQLI_ASSOC);
$Arr[]=$row;
}
print_r($Arr)
如果你是刚开始学php 建议直接抛弃mysql用mysqli 因为PHP5.5已经废弃灶哗mysql方法了
㈣ 如何用html css javascript php制作购物车
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="---.css">
<script src="---.js"></script>
</head>
<body>
<div class="container">
<div class="shop">
<div class="header">
<input type="checkbox" class="shop-checkbox">
<span class="shop-icon"></span>
<span class="shop-name">---</span>
<span class="wangwang-icon"></span>
</div>
<div class="items">
<div class="item">
<div><input class="item-checkbox" type="checkbox" name="" id=""></div>
<div>
<span class="item-img"></span>
</div>
<div class="item-name">
<div>----</div>
<div class="promotion-icons"><span></span><span></span><span></span></div>
</div>
<div class="sku">---</div>
<div class="price">
<div class="price-1">---</div>
<div class="price-2" data-price="---">----</div>
</div>
<div class="num-control">
<span class="num-minus">-</span>
<input class="num" type="text" value="1">
<span class="num-plus">+</span>
</div>
<div class="item-price-total">
<span>¥50.13</span>
</div>
<div class="operation">
<div>---</div>
<div>---</div>
</div>
</div>
</div>
</div>
<div class="shop">
<div class="header">
<input type="checkbox" class="shop-checkbox">
<span class="shop-icon"></span>
<span class="shop-name">---</span>
<span class="wangwang-icon"></span>
</div>
<div class="items">
<div class="item">
<div><input class="item-checkbox" type="checkbox" name="" id=""></div>
<div>
<span class="item-img"></span>
</div>
<div class="item-name">
<div>----</div>
<div class="promotion-icons"><span></span><span></span><span></span></div>
</div>
<div class="sku">---</div>
<div class="price">
<div class="price-1">---</div>
<div class="price-2" data-price="---">---</div>
</div>
<div class="num-control">
<span class="num-minus">-</span>
<input class="num" type="text" value="1">
<span class="num-plus">+</span>
</div>
<div class="item-price-total">
<span>¥9.90</span>
</div>
<div class="operation">
<div>移入收藏夹</div>
<div>删除</div>
</div>
</div>
<div class="item">
<div><input class="item-checkbox" type="checkbox" name="" id=""></div>
<div>
<span class="item-img"></span>
</div>
<div class="item-name">
<div>---</div>
<div class="promotion-icons"><span></span><span></span><span></span></div>
</div>
<div class="sku">---</div>
<div class="price">
<div class="price-1">---</div>
<div class="price-2" data-price="---">---</div>
</div>
<div class="num-control">
<span class="num-minus">-</span>
<input class="num" type="text" value="1">
<span class="num-plus">+</span>
</div>
<div class="item-price-total">
<span>¥19.9</span>
</div>
<div class="operation">
<div>移入收藏夹</div>
<div>删除</div>
</div>
</div>
</div>
</body>
</html>
.container{
width: 1000px;
margin-left: auto;
margin-right: auto;
color: #444444;
}
.header{
margin: 8px;
}
.shop-icon, .wangwang-icon, .promotion-icons span{
display:inline-block;
width: 15px;
height: 15px;
background-color: rgb(117,192,241);
}
.promotion-icons span{
margin-right: 4px;
}
.items{
border: 1px solid #ebe9e9;
}
.item{
display: flex;
margin: 8px;
}
.item-img{
width:100px;
height: 100px;
display: inline-block;
background-color: aquamarine;
margin-left: 6px;
margin-right: 6px;
}
.item-name
{
display: flex;
flex-direction: column;
justify-content: space-between;
}
.sku, .price,.item-price-total,.operation,.num-control{
margin-left: 18px;
}
.num{
width: 18px;
height: 15px;
}
.num-control{
display: flex;
align-items: baseline;
}
.num-minus,.num-plus{
width: 18px;
height: 22px;
display: inline-block;
background-color: #ebe9e9;
}
.checkout{
display: flex;
justify-content: space-between;
}
.shop{
margin-bottom: 20px;
margin-top: 30px;
}
.goods,.freight,.checkout-button{
margin-left: 15px;
}
.price-1{
text-decoration: line-through;
color: gray;
}
.check-num ,.total-price{
font-size: large;
color: red;
margin-left: 3px;
margin-right: 3px;
}
.checkout-button{
height: 30px;
width: 50px;
background-color: beige;
}
.checkout{
margin-top: 15px;
}
.item-name{
width: 25%;
}
.item-price-total{
width: 5%;
}
.sku{
width: 20%;
}
function updatePrice(){
let items = document.querySelectorAll('.item');
let totalNum = 0;
let totalPrice = 0;
items.forEach(function(item){
if(item.querySelector('.item-checkbox').checked){
let num = item.querySelector('.num').value;
totalNum = totalNum + parseInt(num);
let price = item.querySelector('.price-2').getAttribute('data-price');
totalPrice = totalPrice + parseFloat(price) * num;
}
});
document.querySelector('.check-num').innerText= totalNum;
document.querySelector('.total-price').innerText = totalPrice;
}
window.onload=function(){
let itemCheckboxes = document.querySelectorAll('.item-checkbox');
itemCheckboxes.forEach(function(itemCheckbox){
itemCheckbox.onchange = function(){
updatePrice();
}
});
let minuses = document.querySelectorAll('.num-minus');
minuses.forEach(function(minus){
minus.onclick = function(event){
let num=minus.parentElement.querySelector('.num').value;
if(parseInt(num)>1)
{
minus.parentElement.querySelector('.num').value=parseInt(num)-1;
updatePrice();
}
};
});
let pluses = document.querySelectorAll('.num-plus');
pluses.forEach(function(plus){
plus.onclick = function(event){
let num=plus.parentElement.querySelector('.num').value;
plus.parentElement.querySelector('.num').value=parseInt(num)+1;
updatePrice();
}
});
}
㈤ PHP鏁扮粍闂棰桡纴涓涓澶ф暟缁勯噷闱㈢殑灏忔暟缁勶纴濡傛灉鐩稿悓镄刼rderid锛屽垯钖埚苟鎴愪负涓涓鏂扮殑鏁扮粍
寰堢亩鍗旷殑锛屽垎鍒鐢$oldArr,$newArr𨱒ヤ唬琛ㄥ师𨱒ョ殑鏁版嵁鍜岀幇鍦ㄧ殑鏁版嵁锛屾垜涓嬮溃鍙缁椤嚭澶ц嚧镄勯昏緫锛屼綘镊宸卞幓淇鏀瑰苟楠岃瘉钖с
// 铡熸潵镄勬暟缁
$oldArr = array(...);
// 浣犺佺殑鏁扮粍
$newArr = array();
// 杩涜屽惊鐜澶勭悊锛屾敞镒忥纴锲犱负钖埚苟钖庯纴姣忎釜orderid鍙浼氩睘浜庝竴涓鎴愬憳锛屾墍浠ユ柊鏁扮粍镄勬垚锻树互orderid涓洪敭鍊
foreach ($oldArr as $order) {
// 鍒ゆ柇涔嫔墠鏄钖﹀凡缁忓瓨鍦ㄧ浉钖岀殑璁㈠崟锛屼笉鏄鍒欐坊锷犳柊鏁版嵁锛屽凡缁忓瓨鍦ㄥ垯琛ュ厖鐩稿叧鏁版嵁
if (!isset($newArr[$order['orderid']])) {
// 杩欓噷灏辨槸鏂扮殑orderid瀵瑰簲镄勯氱敤鍐呭癸纴涓嶅寘钖鏁板瓧绱㈠紩瀵瑰簲镄勫唴瀹
$newArr[$order['orderid']] = array(
'orderid' => $order['orderid'],
'shopid' => $order['shopid'],
// 缁х画璁剧疆浣犵殑鏁版嵁
);
}
// 杩欓噷璁剧疆閲岄溃鏁板瓧绱㈠紩瀵瑰簲镄勫唴瀹
$newArr[$order['orderid']][] = array(
'sku1' => $order['sku1'],
'remark' => $order['remark'],
// 姝ゅ勭殑阃昏緫灏辩渷鐣ヤ简锛岃嚜宸辨坊锷犲惂
);
}
㈥ php取出多维数组重复的值
$arr = array(
array(
array(
"stocknotifyId" => 1,
"sku" => '13809_BL_37',
"email" => '[email protected]'
),
array(
"stocknotifyId" => 3,
"sku" => '13809_BL_37',
"email" => '[email protected]'
),
array(
"stocknotifyId" => 5,
"sku" => '13809_BL_37',
"email" => '[email protected]'
)
),
array(
array(
"stocknotifyId" => 2,
"sku" => '13809_B_39',
"email" => '[email protected]'
)
),
array(
array(
"stocknotifyId" => 4,
"sku" => '13809_BL_39',
"email" =>'[email protected]'
)
)
);
$re = array();
foreach($arr as $a) {
foreach ($a as $v) {
$tmp_v = $v;
unset($tmp_v['email']);
if(isset($re[$v['email']])) {
$re[$v['email']][] = $tmp_v;
}else{
$re[$v['email']] = array($tmp_v);
}
}
}
print_r($re);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<!-- output -->
Array
(
[[email protected]] => Array
(
[0] => Array
(
[stocknotifyId] => 1
[sku] => 13809_BL_37
)
[1] => Array
(
[stocknotifyId] => 2
[sku] => 13809_B_39
)
)
[[email protected]] => Array
(
[0] => Array
(
[stocknotifyId] => 3
[sku] => 13809_BL_37
)
[1] => Array
(
[stocknotifyId] => 4
[sku] => 13809_BL_39
)
)
[[email protected]] => Array
(
[0] => Array
(
[stocknotifyId] => 5
[sku] => 13809_BL_37
)
)
)