gpt4 book ai didi

PHP Array 删除重复的键值并只显示一个

转载 作者:行者123 更新时间:2023-12-03 23:10:00 25 4
gpt4 key购买 nike

Array
(
[0] => Array
(
[user_id] => 78
[post_id] => 3
[post_user_added_id] => 2
)

[1] => Array
(
[user_id] => 76
[post_id] => 8
[post_user_added_id] => 16
)

[2] => Array
(
[user_id] => 78
[post_id] => 9
[post_user_added_id] => 12
)

[3] => Array
(
[user_id] => 76
[post_id] => 9
[post_user_added_id] => 15
)

[4] => Array
(
[user_id] => 77
[post_id] => 9
[post_user_added_id] => 15
)

)

这里的想法是当有重复的user_id时,它只会显示一个?这是预期的结果:

 Array
(
[2] => Array
(
[user_id] => 78
[post_id] => 9
[post_user_added_id] => 12
)

[3] => Array
(
[user_id] => 76
[post_id] => 9
[post_user_added_id] => 15
)

[4] => Array
(
[user_id] => 77
[post_id] => 9
[post_user_added_id] => 15
)

)

之所以显示[2]键而不是[0]键或[1]键而不是[3]是因为我想获取重复键的底部键。这有点难以解释,但我希望你能理解我所期望的场景或输出。

您的帮助将不胜感激!谢谢! :)

最佳答案

试试这个:

foreach($arr as $k => $v) 
{
foreach($arr as $key => $value)
{
if($k != $key && $v['user_id'] == $value['user_id'])
{
unset($arr[$k]);
}
}
}
print_r($arr);

关于PHP Array 删除重复的键值并只显示一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10301184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com