重复数组不会使用php显示

时间:2018-06-11 14:13:55

标签: php

我有一个数组,我想只显示唯一的" referal_user_id"阵列。在代码中有四个数组,并且有重复的" referal_user_id"我想要一次" referal_user_id"叫不再重复。就像我希望这个数组作为第二个数组。

Array
(
    [0] => stdClass Object
        (
            [id] => 23
            [user_id] => 21
            [referal_user_id] => 16
            [membership_name] => 4
            [points] => 100
            [used_code] => CEMJ916
            [earned_date] => 2018-06-11 06:34:00
        )

    [1] => stdClass Object
        (
            [id] => 24
            [user_id] => 22
            [referal_user_id] => 16
            [membership_name] => 4
            [points] => 400
            [used_code] => CEMJ916
            [earned_date] => 2018-05-31 06:42:07
        )

    [2] => stdClass Object
        (
            [id] => 25
            [user_id] => 16
            [referal_user_id] => 1
            [membership_name] => 2
            [points] => 200
            [used_code] => CEMJ916
            [earned_date] => 2018-06-06 08:36:46
        )

    [3] => stdClass Object
        (
            [id] => 27
            [user_id] => 15
            [referal_user_id] => 1
            [membership_name] => 1
            [points] => 100
            [used_code] => 64FPY1
            [earned_date] => 2018-06-11 06:46:03
        )

)

如何过滤唯一数组 enter image description here

此数组显示为

Array
(
    [0] => stdClass Object
        (
            [id] => 23
            [user_id] => 21
            [referal_user_id] => 16
            [membership_name] => 4
            [points] => 100
            [used_code] => CEMJ916
            [earned_date] => 2018-06-11 06:34:00
        )

    [1] => stdClass Object
        (
            [id] => 25
            [user_id] => 16
            [referal_user_id] => 1
            [membership_name] => 2
            [points] => 200
            [used_code] => CEMJ916
            [earned_date] => 2018-06-06 08:36:46
        )

)

3 个答案:

答案 0 :(得分:1)

您可以使用经典foreach将数组分组为多维数组。使用array_values将多维数组转换为简单数组。

$result = array();
foreach( $arr as $val ) {
    if ( !isset( $result[$val->referal_user_id] ) ) $result[$val->referal_user_id] = $val;
}

$result = array_values( $result );

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

这将导致:

Array
(
    [0] => stdClass Object
        (
            [id] => 23
            [user_id] => 21
            [referal_user_id] => 16
            [membership_name] => 4
            [points] => 100
            [used_code] => CEMJ916
            [earned_date] => 2018-06-11 06:34:00
        )

    [1] => stdClass Object
        (
            [id] => 25
            [user_id] => 16
            [referal_user_id] => 1
            [membership_name] => 2
            [points] => 200
            [used_code] => CEMJ916
            [earned_date] => 2018-06-06 08:36:46
        )

)

答案 1 :(得分:0)

您可以使用in_array函数查找副本,因此该函数将跳过重复,您将获得所需的结果。

答案 2 :(得分:0)

只需创建一个空白数组并将referal_user_id存储在其中,如果该数据不存在于您跳过的数组中,则打印。

e.g。

$unique_array = array();
$unique_referal_ids = array();
foreach($array as $value)
{
  if(!in_array($value->referal_user_id, $unique_referal_ids))
  {
     array_push($unique_array, $value);
     array_push($unique_referal_ids, $value->referal_user_id);  
  }
}

print_r($unique_array);

你会得到你想要的结果。