从同一阵列与别人比较值的数组值 [英] Compare array values with others values from the same array
问题描述
我想要实现的是,它会循环槽的阵列。然后,它会看,如果阵列中的项目是关于三个点是相同的:PRODUCT_ID,尺寸值和颜色值。
我想创造一个项目都列一个新的数组,我不希望的唯一的事情就是重复值。我想了重复的值,如果他们在这三个点的数量将一起计算相同的。就像如果我有3项相同的产品ID相同的尺寸和相同的颜色和三者并重的我下令我的新阵列3项这只是站在1时间和数量将是9于是就有了我新的不重复值数组。
电流回路
的foreach($订单为$关键=> $顺序){
的foreach($秩序['orderProducts']为$关键=> $值){
呼应'< pre>';
的print_r($值['属性']);
呼应'< / pre>';
}
}
结果如下数组
阵列
(
[ID] => 2
[PRODUCT_ID] => 4
[ORDER_ID] => 2
[名] => swag3
[说明] =>哈哈
[价格] => 19.95
[收益] => 10.00
[数量] => 2
[属性] => [{ID:1,名:大小,值:XS,主动:1},{ID:8,名:颜色,价值: 才子,主动:1}]
)
排列
(
[ID] => 3
[PRODUCT_ID] => 3
[ORDER_ID] => 3
[名] => swag2
[说明] =>大声笑
[价格] => 19.95
[收益] => 10.00
[数量] => 2
[属性] => [{ID:2,名:大小,值:S,主动:1},{ID:7,名:颜色,价值: 兹瓦特,活性:1}]
)
排列
(
[ID] => 4
[PRODUCT_ID] => 3
[ORDER_ID] => 4
[名] => swag2
[说明] =>大声笑
[价格] => 19.95
[收益] => 10.00
[数量] => 1
[属性] => [{ID:2,名:大小,值:S,主动:1},{ID:7,名:颜色,价值: 兹瓦特,活性:1}]
)
排序的我要找的。 P>
阵列
(
[ID] => 2
[PRODUCT_ID] => 4
[ORDER_ID] => 2
[名] => swag3
[说明] =>哈哈
[价格] => 19.95
[收益] => 10.00
[数量] => 2
[属性] => [{ID:1,名:大小,值:XS,主动:1},{ID:8,名:颜色,价值: 才子,主动:1}]
)
排列
(
[ID] => 3
[PRODUCT_ID] => 3
[ORDER_ID] => 3
[名] => swag2
[说明] =>大声笑
[价格] => 19.95
[收益] => 10.00
[数量] => 3
[属性] => [{ID:2,名:大小,值:S,主动:1},{ID:7,名:颜色,价值: 兹瓦特,活性:1}]
)
解决方案
注意它的刀片PHP作为前端。
后端
$为了//与产品行列
$项= [];
的foreach($订单为$关键=> $顺序){
的foreach($秩序['orderProducts']为$ OP){
$ I = [
'产品'= GT;产品:: findOrFail($ OP-GT&; PRODUCT_ID) - GT;的toArray()
'属性'=> $ OP-GT&;属性,
量= GT; $ OP-GT&;数量
];
$ matchedResult = FALSE;
$数= COUNT($项目);
为($ A = 0; $ A< $计数; $一个++){
//使用$项数组在同一PRODUCT_ID项目
如果($项目[$一] ['产品'] ['身份证'] == $ I ['产品'] ['身份证']){
//检查属性也相同
如果($项目[$一] ['属性'] === $ I ['属性']){
//属性AR埃特相同的,所以补上
$项目[$一] ['量'] + = $ I ['量'];
$ matchedResult =真;
继续; //如果其右侧没有其他比赛
}
}
}
如果($ matchedResult === FALSE){
//仅当存在不匹配推项目。
$项目[] = $ I;
}
}
}
前端
< DIV CLASS =表响应>
<表类=表的表条纹>
<&THEAD GT;
&所述; TR>
<第i产品与LT; /第i
<第i量和LT; /第i
< / TR>
< / THEAD>
<&TBODY GT;
@foreach($项目为$项)
&所述; TR>
< TD> {{$项目[产品] [名称]}}
@if(计数($项目['属性'])0)<小>
@foreach($项目['属性']为$ ATT)
{{$ ATT [名称]}} - {{$ ATT ['值']}}
@endforeach
< /小>
@ ENDIF< / TD>
< TD> {{$项目[数量]}}< / TD>
< / TR>
@endforeach
< / TBODY>
< /表>
< / DIV>
您可以实现自己的目标,而无需使用嵌套循环。您可以使用的PRODUCT_ID,大小和颜色参数散列函数,并使用该值作为这样一个新的数组键:
$ =订单//原数组;
$ newOrders = []; //新数组的foreach($订单为$顺序){
$ PI = $秩序[的product_id]; //获取PRODUCT_ID
$ ATTR = json_de code($秩序[属性]); //获取属性:
$大小= $ ATTR [0] - >价值; //获取大小值
$颜色= $ ATTR [1] - >颜色; //获取颜色 $哈希= sprintf的(%s%s%S。$ PI,$大小,颜色$); //计算散列 如果($ newOrders [$哈希]){
$ newOrders [$哈希] .quantity ++; //如果散列已经present然后只是增加量
}其他{
//否则添加新秩序
$ newOrders [$哈希] = [
命令=> $秩序,
量= GT; 1
];
}
}
What I’m trying to achieve is that, it will loop trough the array. Then it will look if the items in the array are the same on three points: product_id, the size value and the color value. I want to create a new array where the items are listed, the only thing I don’t want is the duplicated values. I want that the duplicated values if they are the same on those three points that the quantity will be count together. Like if I have 3 items same product id same size and same color and both of the three I ordered 3 items in my new array this is just standing 1 time and the quantity will be 9. So there will be no duplicated values in my new array.
Current loop
foreach($orders as $key => $order){
foreach($order['orderProducts'] as $key => $value){
echo '<pre>';
print_r($value['attributes']);
echo '</pre>';
}
}
results in the the following array
Array
(
[id] => 2
[product_id] => 4
[order_id] => 2
[name] => swag3
[description] => haha
[price] => 19.95
[proceeds] => 10.00
[quantity] => 2
[attributes] => [{"id":1,"name":"Size","value":"XS","active":1},{"id":8,"name":"Color","value":"Wit","active":1}]
)
Array
(
[id] => 3
[product_id] => 3
[order_id] => 3
[name] => swag2
[description] => lol
[price] => 19.95
[proceeds] => 10.00
[quantity] => 2
[attributes] => [{"id":2,"name":"Size","value":"S","active":1},{"id":7,"name":"Color","value":"Zwart","active":1}]
)
Array
(
[id] => 4
[product_id] => 3
[order_id] => 4
[name] => swag2
[description] => lol
[price] => 19.95
[proceeds] => 10.00
[quantity] => 1
[attributes] => [{"id":2,"name":"Size","value":"S","active":1},{"id":7,"name":"Color","value":"Zwart","active":1}]
)
Sort of what I’m looking for..
Array
(
[id] => 2
[product_id] => 4
[order_id] => 2
[name] => swag3
[description] => haha
[price] => 19.95
[proceeds] => 10.00
[quantity] => 2
[attributes] => [{"id":1,"name":"Size","value":"XS","active":1},{"id":8,"name":"Color","value":"Wit","active":1}]
)
Array
(
[id] => 3
[product_id] => 3
[order_id] => 3
[name] => swag2
[description] => lol
[price] => 19.95
[proceeds] => 10.00
[quantity] => 3
[attributes] => [{"id":2,"name":"Size","value":"S","active":1},{"id":7,"name":"Color","value":"Zwart","active":1}]
)
Solution Note it's blade php as frontend.
Backend
$order // is the array with products
$items = [];
foreach($orders as $key => $order){
foreach($order['orderProducts'] as $op){
$i = [
'product'=> Product::findOrFail($op->product_id)->toArray(),
'attributes' =>$op->attributes,
'quantity'=>$op->quantity
];
$matchedResult = false;
$count = count($items);
for($a = 0; $a < $count; $a++){
// Items with the same product_id in the $item array
if($items[$a]['product']['id'] == $i['product']['id']){
//check if the attributes are also the same
if($items[$a]['attributes'] === $i['attributes']){
// The attributes ar ethe same so up the quantity
$items[$a]['quantity'] += $i['quantity'];
$matchedResult = true;
continue; // If its right there are no other matches
}
}
}
if($matchedResult === false){
// only push item if there is not a match.
$items[] = $i;
}
}
}
Frontend
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>Product</th>
<th>quantity</th>
</tr>
</thead>
<tbody>
@foreach($items as $item)
<tr>
<td>{{$item['product']['name']}}
@if(count($item['attributes']) > 0) <small>
@foreach($item['attributes'] as $att)
{{$att['name']}} - {{$att['value']}}
@endforeach
</small>
@endif</td>
<td>{{$item['quantity']}}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
You can achieve your goal without using nested loops. You may use hash function of product_id, size and color parameters and use that value as a new array key like this:
$orders = // original array;
$newOrders = []; // new array
foreach($orders as $order) {
$pi = $order["product_id"]; // get product_id
$attr = json_decode($order["attributes"]); // get attributes:
$size = $attr[0]->value; // get size value
$color = $attr[1]->Color; // get color
$hash = sprintf("%s.%s.%s", $pi, $size, $color); // Calculate hash
if ($newOrders[$hash]) {
$newOrders[$hash].quantity++; // If hash is already present then just increase quantity
} else {
// Otherwise add new order
$newOrders[$hash] = [
"order" => $order,
"quantity" => 1
];
}
}
这篇关于从同一阵列与别人比较值的数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!