php数组根据某键值,把相同键值的合并最终生成一个新的二维数组
待处理数组array([0]=>array('id'=>'1','names'=>'jack','0'=>'sendone'),[1]=>array('id'=>'3',...
待处理数组
array(
[0]=>array(
'id'=>'1',
'names'=>'jack',
'0'=>'sendone'
),
[1]=>array(
'id'=>'3',
'names'=>'tree',
'0'=>'sendtwo'
),
[2]=>array(
'id'=>'5',
'names'=>'apple',
'0'=>'sendtwo'
),
);
需要把相同的合成一个新数组(比如例子中'0'=>'sendtwo'的),键值未知,处理后
array(
[0]=>array(
[0]=>array(
'id'=>'1',
'names'=>'jack',
'0'=>'sendone'
),
[1]=>array(
[0]=>array(
'id'=>'3',
'names'=>'tree',
'0'=>'sendtwo'
),
[1]=>array(
'id'=>'5',
'names'=>'apple',
'0'=>'sendtwo'
),
),
),
); 展开
array(
[0]=>array(
'id'=>'1',
'names'=>'jack',
'0'=>'sendone'
),
[1]=>array(
'id'=>'3',
'names'=>'tree',
'0'=>'sendtwo'
),
[2]=>array(
'id'=>'5',
'names'=>'apple',
'0'=>'sendtwo'
),
);
需要把相同的合成一个新数组(比如例子中'0'=>'sendtwo'的),键值未知,处理后
array(
[0]=>array(
[0]=>array(
'id'=>'1',
'names'=>'jack',
'0'=>'sendone'
),
[1]=>array(
[0]=>array(
'id'=>'3',
'names'=>'tree',
'0'=>'sendtwo'
),
[1]=>array(
'id'=>'5',
'names'=>'apple',
'0'=>'sendtwo'
),
),
),
); 展开
展开全部
php数组根据某一个键值,把相同键值的合并生成一个新的二维数组
源数据:
$infos = array( array( 'a' => 36, 'b' => 'xa', 'c' => '2015-08-28 00:00:00', 'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
), array( 'a' => 3, 'b' => 'vd', 'c' => '2015-08-20 00:00:00', 'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
), array( 'a' => 6, 'b' => 'wwe', 'c' => '2015-08-28 00:00:00', 'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
), array( 'a' => 36, 'b' => 'se', 'c' => '2015-08-28 00:00:00', 'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
), array( 'a' => 6, 'b' => 'aw', 'c' => '2015-08-28 00:00:00', 'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
), array( 'a' => 36, 'b' => 'bv', 'c' => '2015-08-28 00:00:00', 'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
), array( 'a' => 12, 'b' => 'xx', 'c' => '2015-08-27 00:00:00', 'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
)
);1234567891011121314151617181920212223242526272829303132333435363738394041424344
处理要求:将数组中a相同的二维数组合并到一个数组中,生成一个新的数组
代码:
<?php
$result= array(); foreach ($infos as $key => $info) { $result[$info['a']][] = $info;
}
print_r($result);123456
输出:
Array(
[36] => Array
(
[0] => Array
(
[a] => 36
[b] => xa
[c] => 2015-08-28 00:00:00
[d] => 2015/08/438488a00b3219929282e3652061c2e3.png
)
[1] => Array
(
[a] => 36
[b] => se
[c] => 2015-08-28 00:00:00
[d] => 2015/08/438488a00b3219929282e3652061c2e3.png
)
[2] => Array
(
[a] => 36
[b] => bv
[c] => 2015-08-28 00:00:00
[d] => 2015/08/438488a00b3219929282e3652061c2e3.png
)
)
[3] => Array
(
[0] => Array
(
[a] => 3
[b] => vd
[c] => 2015-08-20 00:00:00
[d] => 2015/08/438488a00b3219929282e3652061c2e3.png
)
)
[6] => Array
(
[0] => Array
(
[a] => 6
[b] => wwe
[c] => 2015-08-28 00:00:00
[d] => 2015/08/438488a00b3219929282e3652061c2e3.png
)
[1] => Array
(
[a] => 6
[b] => aw
[c] => 2015-08-28 00:00:00
[d] => 2015/08/438488a00b3219929282e3652061c2e3.png
)
)
[12] => Array
(
[0] => Array
(
[a] => 12
[b] => xx
[c] => 2015-08-27 00:00:00
[d] => 2015/08/438488a00b3219929282e3652061c2e3.png
)
)
)
源数据:
$infos = array( array( 'a' => 36, 'b' => 'xa', 'c' => '2015-08-28 00:00:00', 'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
), array( 'a' => 3, 'b' => 'vd', 'c' => '2015-08-20 00:00:00', 'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
), array( 'a' => 6, 'b' => 'wwe', 'c' => '2015-08-28 00:00:00', 'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
), array( 'a' => 36, 'b' => 'se', 'c' => '2015-08-28 00:00:00', 'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
), array( 'a' => 6, 'b' => 'aw', 'c' => '2015-08-28 00:00:00', 'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
), array( 'a' => 36, 'b' => 'bv', 'c' => '2015-08-28 00:00:00', 'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
), array( 'a' => 12, 'b' => 'xx', 'c' => '2015-08-27 00:00:00', 'd' => '2015/08/438488a00b3219929282e3652061c2e3.png'
)
);1234567891011121314151617181920212223242526272829303132333435363738394041424344
处理要求:将数组中a相同的二维数组合并到一个数组中,生成一个新的数组
代码:
<?php
$result= array(); foreach ($infos as $key => $info) { $result[$info['a']][] = $info;
}
print_r($result);123456
输出:
Array(
[36] => Array
(
[0] => Array
(
[a] => 36
[b] => xa
[c] => 2015-08-28 00:00:00
[d] => 2015/08/438488a00b3219929282e3652061c2e3.png
)
[1] => Array
(
[a] => 36
[b] => se
[c] => 2015-08-28 00:00:00
[d] => 2015/08/438488a00b3219929282e3652061c2e3.png
)
[2] => Array
(
[a] => 36
[b] => bv
[c] => 2015-08-28 00:00:00
[d] => 2015/08/438488a00b3219929282e3652061c2e3.png
)
)
[3] => Array
(
[0] => Array
(
[a] => 3
[b] => vd
[c] => 2015-08-20 00:00:00
[d] => 2015/08/438488a00b3219929282e3652061c2e3.png
)
)
[6] => Array
(
[0] => Array
(
[a] => 6
[b] => wwe
[c] => 2015-08-28 00:00:00
[d] => 2015/08/438488a00b3219929282e3652061c2e3.png
)
[1] => Array
(
[a] => 6
[b] => aw
[c] => 2015-08-28 00:00:00
[d] => 2015/08/438488a00b3219929282e3652061c2e3.png
)
)
[12] => Array
(
[0] => Array
(
[a] => 12
[b] => xx
[c] => 2015-08-27 00:00:00
[d] => 2015/08/438488a00b3219929282e3652061c2e3.png
)
)
)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
foreach($sourceArray as $k=>$v) {
$result[$v["sendto"]][] = $v; // sendto 根据你想要的
}
$result[$v["sendto"]][] = $v; // sendto 根据你想要的
}
更多追问追答
追问
谢谢回答。
不过键值是不固定的,但是所有键名都是固定的,就是判断键名是0的键值相同的(比如例子中'0'=>'sendtwo'的),放到一个数组里;其他键名的也单独放在一个数组。标题表达错了,最终的数组应该是三维的~
追答
一样,键值键名不固定都无所谓。只要改$v["sendto"] 这里把sendto改成你的键名就行了。。 你先试试再说。。。。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询