0

I have an array, I want to show only unique "referal_user_id" array. In the code have four array and there are repeated "referal_user_id" i want when one time "referal_user_id" called then not repeated again. Like i want this array as second array.

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
        )

)

How can i filter unique array enter image description here

This array show like

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
        )

)
6
  • Can you please show your code? Commented Jun 11, 2018 at 14:16
  • $query = $wpdb->get_results("SELECT * FROM wp_pmpro_earn_points"); echo '<pre>'; print_r($query); echo '</pre>'; Commented Jun 11, 2018 at 14:17
  • You can do it directly in the query with something like this. You just have to adapt it to your database engine. Commented Jun 11, 2018 at 14:19
  • this is my table prntscr.com/jtnoob Commented Jun 11, 2018 at 14:19
  • can you please correct my code Commented Jun 11, 2018 at 14:20

3 Answers 3

1

You can use the classic foreach to group the array into a multidimensional array. Use array_values to convert the multidimensional array to simple array.

$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>";

This will result to:

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
        )

)
Sign up to request clarification or add additional context in comments.

1 Comment

@HusainAhmed Happy to help :)
0

You can use in_array function to find the duplicate so in that function will skip duplicate and you will get your desired result.

Comments

0

just create a blank array and store the referal_user_id in it if that id dosen't exist in array you skip else print.

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);

and you'll get your desired result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.