数据:
array(3) {
[0] => array(17) {
["id"] => string(2) "1"
["order_code"] => string(15) "AAA"
}
[1] => array(17) {
["id"] => string(2) "2"
["order_code"] => string(15) "AAA"
}
[2] => array(17) {
["id"] => string(2) "3"
["order_code"] => string(15) "BBB"
}
}结果:
array(3) {
[0] =>{
[0] => array(17) {
["id"] => string(2) "1"
["order_code"] => string(15) "AAA"
}
[1] => array(17) {
["id"] => string(2) "2"
["order_code"] => string(15) "AAA"
}
}
[1]=>{
[0] => array(17) {
["id"] => string(2) "3"
["order_code"] => string(15) "BBB"
}
}
我的解决方法 有个小BUG,而且数据多的时候 很卡 ,有没有解决的方法?
foreach ($lists as $key => $val) {
if (in_array($val['order_code'], $show)) {
foreach ($array as $i => $v) {
foreach ($v as $n) {
if ($n['order_code'] == $val['order_code']) {
$array[$i][] = $val;
}
}
}
} else {
$array[$key][] = $val;
$show[] = $val['order_code'];
}
}
array(3) {
[0] => array(17) {
["id"] => string(2) "1"
["order_code"] => string(15) "AAA"
}
[1] => array(17) {
["id"] => string(2) "2"
["order_code"] => string(15) "AAA"
}
[2] => array(17) {
["id"] => string(2) "3"
["order_code"] => string(15) "BBB"
}
}结果:
array(3) {
[0] =>{
[0] => array(17) {
["id"] => string(2) "1"
["order_code"] => string(15) "AAA"
}
[1] => array(17) {
["id"] => string(2) "2"
["order_code"] => string(15) "AAA"
}
}
[1]=>{
[0] => array(17) {
["id"] => string(2) "3"
["order_code"] => string(15) "BBB"
}
}
我的解决方法 有个小BUG,而且数据多的时候 很卡 ,有没有解决的方法?
foreach ($lists as $key => $val) {
if (in_array($val['order_code'], $show)) {
foreach ($array as $i => $v) {
foreach ($v as $n) {
if ($n['order_code'] == $val['order_code']) {
$array[$i][] = $val;
}
}
}
} else {
$array[$key][] = $val;
$show[] = $val['order_code'];
}
}
array(
"id" => "1",
"order_code" => "AAA"
),
array(
"id" => "2",
"order_code" => "AAA"
),
array(
"id" => "3",
"order_code" => "BBB"
)
);
foreach($a as $v)
$r[$v['order_code']][] = $v;
print_r(array_values($r));Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[order_code] => AAA
) [1] => Array
(
[id] => 2
[order_code] => AAA
) ) [1] => Array
(
[0] => Array
(
[id] => 3
[order_code] => BBB
) ))