Search code examples
phparraysmergearray-unique

How to array_unique dimensional arrays


How to array_unique dimensional arrays I have array below :

Input Array

[0] => Array
    (
        [staff_id] => 1
        [service_id] => 1
        [avatar_url] => gallery-1.png
        [full_name] => Germaine
        [price] => 0.00
    )

[1] => Array
    (
        [staff_id] => 3
        [service_id] => 1
        [avatar_url] => team-5.png
        [full_name] => Jenny
        [price] => 0.00
    )

[2] => Array
    (
        [staff_id] => 5
        [service_id] => 1
        [avatar_url] => gallery-6.png
        [full_name] => Shawn
        [price] => 0.00
    )

[3] => Array
    (
        [staff_id] => 2
        [service_id] => 6
        [avatar_url] => team-3.png
        [full_name] => Gabriel
        [price] => 0.00
    )

[4] => Array
    (
        [staff_id] => 3
        [service_id] => 6
        [avatar_url] => team-5.png
        [full_name] => Jenny
        [price] => 0.00
    )

And i want have result:

Expected Output Array

[0] => Array
    (
        [staff_id] => 1
        [service_id] => 1
        [avatar_url] => gallery-1.png
        [full_name] => Germaine
        [price] => 0.00
    )

[1] => Array
    (
        [staff_id] => 3
        [service_id] => 1
        [avatar_url] => team-5.png
        [full_name] => Jenny
        [price] => 0.00
    )

[2] => Array
    (
        [staff_id] => 5
        [service_id] => 1
        [avatar_url] => gallery-6.png
        [full_name] => Shawn
        [price] => 0.00
    )

[3] => Array
    (
        [staff_id] => 2
        [service_id] => 6
        [avatar_url] => team-3.png
        [full_name] => Gabriel
        [price] => 0.00
    )

Can someone help me. How to do that ?. I use array_unique but it not woking with dimensional arrays. How to resolve this ? Thanks u


Solution

  • You can do this:

    $output_array = array();
    $arr_temp_id = array();
    foreach($your_array as $arr)
    {
          if(!in_array($arr['staff_id'], $arr_temp_id))
          {
              $arr_temp_id[] = $arr['staff_id'];
              $output_array[] = $arr;
          }
    }
    
    print_r($output_array);