php数组循环遍历成1维数组并排重

奔波在生活路上 程序猿  2021-03-17 02:15:35
像这种数据,我想遍历成1维数组应该怎么写呢?

Array
(
[0] => Array
(
[0] => Array
(
[uid] => 22
[username] => 13500000000
[bid] => 13
)
[1] => Array
(
[uid] => 29
[username] => 13111111111
[bid] => 13
)
)
[1] => Array
(
[0] => Array
(
[uid] => 22
[username] => 13500000000
[bid] => 81
)
[1] => Array
(
[uid] => 29
[username] => 13111111111
[bid] => 81
)
[2] => Array
(
[uid] => 32
[username] => 13222222222
[bid] => 81
)
)
)



想变成这样

Array
(
[0] => Array
(
[uid] => 22
[username] => 13500000000
[bid] => 13
)
[1] => Array
(
[uid] => 29
[username] => 13111111111
[bid] => 13
)
[2] => Array
(
[uid] => 32
[username] => 13708064094
[bid] => 81
)
)





...全文
58 5 点赞 打赏 收藏 举报
写回复
5 条回复
切换为时间正序
请发表友善的回复…
发表回复

var_dump(array_merge(...$arr));
这样就可以了 至于这个用法 需要PHP5.6+ 具体看看 https://www.php.net/manual/zh/functions.arguments.php#functions.variable-arg-list 示例 #10
  • 打赏
  • 举报
回复
谢谢各位大大解答,我自己捣鼓出来了

foreach ($list as $key => $value) {
		foreach ($value as $k => $v) {
                      if (in_array($v[$k]['uid'], $arrs)) {
				unset($arrs[$k]);
			} else {
				$arrs[$k] = $v;
			}

                }
}

  • 打赏
  • 举报
回复
Stephen_112 2021-03-17
Array ( [0] => Array ( [0] => Array ( [uid] => 22 [username] => 13500000000 [bid] => 81 ) [1] => Array ( [uid] => 29 [username] => 13111111111 [bid] => 13 ) ) [1] => Array ( [0] => Array ( [uid] => 22 [username] => 13500000000 [bid] => 81 ) [1] => Array ( [uid] => 29 [username] => 13111111111 [bid] => 81 ) [2] => Array ( [uid] => 32 [username] => 13222222222 [bid] => 81 ) ) ) Array ( [0] => Array ( [uid] => 22 [username] => 13500000000 [bid] => 81 ) [1] => Array ( [uid] => 29 [username] => 13111111111 [bid] => 13 ) [2] => Array ( [uid] => 22 [username] => 13500000000 [bid] => 81 ) [3] => Array ( [uid] => 29 [username] => 13111111111 [bid] => 81 ) [4] => Array ( [uid] => 32 [username] => 13222222222 [bid] => 81 ) )
  • 打赏
  • 举报
回复
Stephen_112 2021-03-17
3维数组变成2维么

$arr = array(
	array(
		array(
			'uid' => 22,
			'username' => 13500000000,
			'bid' => 81
		),
		array(
			'uid' => 29,
			'username' => 13111111111,
			'bid' => 13
		)
	),
	array(
		array(
			'uid' => 22,
			'username' => 13500000000,
			'bid' => 81
		),
		array(
			'uid' => 29,
			'username' => 13111111111,
			'bid' => 81
		),
		array(
			'uid' => 32,
			'username' => 13222222222,
			'bid' => 81
		)
	),
);

$final = [];

foreach($arr as $value){
	foreach($value as $val){
		$final[] = $val;
	}
}

echo "<pre>";
print_r($arr);
echo "</pre>";

echo "<pre>";
print_r($final);
echo "</pre>";
  • 打赏
  • 举报
回复
qq_39047451 2021-03-17
循环数组 创建一个 数组 用 uid值.‘-’.username值.‘-’.bid值 为key 当前数组为balue 往这个新数组插入 或者isset再插 循环完,这个新数组直接array_values就是你要的数组
  • 打赏
  • 举报
回复
相关推荐
发帖
基础编程
加入

2.1w+

社区成员

从PHP安装配置,PHP入门,PHP基础到PHP应用
申请成为版主
帖子事件
创建了帖子
2021-03-17 02:15
社区公告
暂无公告