php将二维数组值数组里某一个键值一样的拼装成一个三维数组 5
array(3){[0]=>array(3){["order_id"]=>string(1)"1"["goods_id"]=>string(1)"1"["goods_na...
array(3) {
[0] => array(3) {
["order_id"] => string(1) "1"
["goods_id"] => string(1) "1"
["goods_name"] => string(12) "雄风一号"
}
[1] => array(3) {
["order_id"] => string(1) "2"
["goods_id"] => string(1) "1"
["goods_name"] => string(12) "雄风一号"
}
[2] => array(3) {
["order_id"] => string(1) "1"
["goods_id"] => string(1) "2"
["goods_name"] => string(12) "雄风一号"
}
}
我想弄成
array(2) {
[0] => array(2) {
[0] => array(3) {
["order_id"] => string(1) "1"
["goods_id"] => string(1) "1"
["goods_name"] => string(12) "雄风一号"
}
[1] => array(3) {
["order_id"] => string(1) "1"
["goods_id"] => string(1) "2"
["goods_name"] => string(12) "雄风一号"
}
}
[1] => array(3) {
["order_id"] => string(1) "2"
["goods_id"] => string(1) "1"
["goods_name"] => string(12) "雄风一号"
}
}
求高手解决 展开
[0] => array(3) {
["order_id"] => string(1) "1"
["goods_id"] => string(1) "1"
["goods_name"] => string(12) "雄风一号"
}
[1] => array(3) {
["order_id"] => string(1) "2"
["goods_id"] => string(1) "1"
["goods_name"] => string(12) "雄风一号"
}
[2] => array(3) {
["order_id"] => string(1) "1"
["goods_id"] => string(1) "2"
["goods_name"] => string(12) "雄风一号"
}
}
我想弄成
array(2) {
[0] => array(2) {
[0] => array(3) {
["order_id"] => string(1) "1"
["goods_id"] => string(1) "1"
["goods_name"] => string(12) "雄风一号"
}
[1] => array(3) {
["order_id"] => string(1) "1"
["goods_id"] => string(1) "2"
["goods_name"] => string(12) "雄风一号"
}
}
[1] => array(3) {
["order_id"] => string(1) "2"
["goods_id"] => string(1) "1"
["goods_name"] => string(12) "雄风一号"
}
}
求高手解决 展开
2个回答
展开全部
<?php
$indexArr = array();
$newArr = array();
foreach ($sourceArr as $v) {
// 判断该order_id是否已经存在于新数组中
if (array_key_exists($v['order_id'], $indexArr)) {
// 获取该order_id在新数组中的位置
$k = $indexArr[$v['order_id']];
// 如果该部分没有被加维
if (!array_key_exists('order_id', $newArr[$k])) {
// 加维,并且将这一条记录插入
$newArr[$k] = array($newArr[$k], $v);
} else {
array_push($newArr[$k], $v);
}
} else {
$key = count($newArr); // 插入新数组的位置
array_push($newArr, $v);
// 建立一个order_id对应其在新数组中位置的索引数组
$indexArr[$v['order_id']] = $key;
}
}
这是按照你要的结构给你组织的。不过你要的结构可能并不方便你进行处理,因为订单号相同的记录比订单号唯一的记录维度要深一层,如果你要维度相同的话,可以这么做
$indexArr = array();
$newArr = array();
foreach ($sourceArr as $v) {
if (array_key_exists($v['order_id'], $indexArr)) {
$k = $indexArr[$v['order_id']];
array_push($newArr[$k], $v);
} else {
$key = count($newArr);
array_push($newArr, array($v));
$indexArr[$v['order_id']] = $key;
}
}
展开全部
$arr = array(
array(
"order_id" => "1",
"goods_id" => "1",
"goods_name" => "雄风一号"
),
array(
"order_id" => "2",
"goods_id" => "1",
"goods_name" => "雄风一号"
),
array(
"order_id" => "1",
"goods_id" => "2",
"goods_name" => "雄风一号"
)
);
$tmp = array();
foreach($arr as $v){ //将原数组重新组合为同一order_id在order_id为键名的临时数组里
$tmp[$v['order_id']][]=$v;
}
ksort($tmp); //将临时数组按order_id排序
$new = array();
foreach($tmp as $v){ //将临时数组按顺序赋值到新数组里,新数组以自然数字为键名
$new[] = $v;
}
print_r($new);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询