PHP数组删除重复的键值并仅显示一个
这里的想法是,当有一个重复的用户id时,它只会显示一个?这是预期的结果:PHP数组删除重复的键值并仅显示一个,php,multidimensional-array,Php,Multidimensional Array,这里的想法是,当有一个重复的用户id时,它只会显示一个?这是预期的结果: Array ( [0] => Array ( [user_id] => 78 [post_id] => 3 [post_user_added_id] => 2 ) [1] => Array ( [user_id] => 76
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
)
)
显示[2]键而不是[0]键或[1]键而不是[3]键的原因,因为我想获取重复键的底部键。这有点难以解释,但我希望您理解我期望的场景或输出
非常感谢您的帮助!谢谢!:) 试试这个:
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
)
)
试一试
输出
$array = 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
)
);
$keys = array ();
// Get Position
foreach ( $array as $key => $value ) {
$keys [$value ['user_id']] = $key;
}
// Remove Duplicate
foreach ( $array as $key => $value ) {
if (! in_array ( $key, $keys )) {
unset ( $array [$key] );
}
}
var_dump ( $array );
您可以使用foreach循环来覆盖用户id。我认为这样应该可以
array
2 =>
array
'user_id' => int 78
'post_id' => int 9
'post_user_added_id' => int 12
3 =>
array
'user_id' => int 76
'post_id' => int 9
'post_user_added_id' => int 15
4 =>
array
'user_id' => int 77
'post_id' => int 9
'post_user_added_id' => int 15
以下内容对我有用,用于清洁以下内容:
$new = array();
foreach ($array as $value)
{
$new[$value['user_id']] = $value;
}
print_r($new);
代码:
用于($i=0;$i到目前为止您尝试了什么?—请发布您的代码,并明确指出您遇到了什么障碍。我没有尝试任何东西,因为我真的不知道执行预期输出的逻辑。此网站是为程序员提供的,因此请参阅此处的常见问题解答,询问哪些问题。您考虑过将数组移动到d中吗atabase?我是一名程序员,这就是我提出PHP数组值的原因。我只是问如何删除数组中的重复值。您留下的前一个数组工作得很好!不需要数组_reverse()它,因为我想得到重复键值的底键。:)您可能需要反转初始数组,因为这不是所需的输出。
$new = array();
foreach ($array as $value)
{
$new[$value['user_id']] = $value;
}
print_r($new);
$arr = array(
array("ID"=>"234"),
array("ID"=>"235"),
array("ID"=>"236"),
array("ID"=>"236"),
array("ID"=>"234"),
);
for ($i=0; $i <count($arr) ; $i++) {
$distinct_id = $arr[$i]["ID"]; // this is the ID for which we want to eliminate the duplicates
$position = $i; // position of that array (so we ignore it)
$unset_arr = array(); // these duplicate entries will be removed
// Collect list of duplicates to remove
for ($y=0; $y <count($arr) ; $y++) {
// Skip the position of the array that we're checking (we want to remove duplicate not the original)
if($y == $position)
continue;
// If found remove and reset keys
if($arr[$y]["ID"] == $distinct_id) {
$unset_arr[] = $y;
}
}
// Remove IDs collected in the loop above
foreach ($unset_arr as $k) {
unset($arr[$k]);
}
// Reset keys
$arr = array_values($arr);
}