<?php
//原数组
$arr = [
['product_id'=>33, 'seller_id'=>8],
['product_id'=>32, 'seller_id'=>7],
['product_id'=>31, 'seller_id'=>6],
['product_id'=>33, 'seller_id'=>5],
['product_id'=>33, 'seller_id'=>8],
];
$p = [];
$pp = [];
if(!empty($arr)){
//结果1
foreach($arr as $v){
isset($p[$v['product_id']]) || $p[$v['product_id']] = [];
$p[$v['product_id']][] = $v['seller_id'];
$pp[$v['product_id']] .= $v['seller_id'].',';
}
//结果2
foreach($pp as &$v1){
$v1 = trim($v1, ',');
}
}
//打印输出分别为
//打印1
echo '<pre>';
print_r($p);
echo '</pre>';
//输出1
Array
(
[33] => Array
(
[0] => 8
[1] => 5
[2] => 8
)
[32] => Array
(
[0] => 7
)
[31] => Array
(
[0] => 6
)
)
//打印2
echo '<pre>';
print_r($pp);
echo '</pre>';
//输出2
Array
(
[33] => 8,5,8
[32] => 7
[31] => 6
)
?>
php 数组中相同值的组成新的数组
最新推荐文章于 2022-02-15 16:26:51 发布