0

I have three keys pointing to value of type array, in those array values I have duplicate values. How can I remove them?

array(3) {
["rock"]=>
array(4) {
[0]=>
array(9) {
  ["id"]=>
  string(1) "1"
  ["title"]=>
  string(15) "Teleman Tickets"
  ["location"]=>
  string(20) "Concorde 2, Brighton"
  ["event_date"]=>
  string(10) "2017-02-20"
  ["event_time"]=>
  string(8) "20:00:00"
  ["url"]=>
  string(1) "0"
  ["geo_lat"]=>
  string(18) "50.817321799999990"
  ["geo_long"]=>
  string(17) "-0.12304610000001"
  ["tags"]=>
  string(30) "rock,alternative,indie"
}
[1]=>
array(9) {
  ["id"]=>
  string(1) "4"
  ["title"]=>
  string(9) "Blink-182"
  ["location"]=>
  string(25) "Motorpoint Arena, Cardiff"
  ["event_date"]=>
  string(10) "2017-07-03"
  ["event_time"]=>
  string(8) "18:00:00"
  ["url"]=>
  string(1) "0"
  ["geo_lat"]=>
  string(18) "51.478937400000010"
  ["geo_long"]=>
  string(17) "-3.17172289999996"
  ["tags"]=>
  string(14) "rock,metal"
}
[2]=>
array(9) {
  ["id"]=>
  string(1) "8"
  ["title"]=>
  string(5) "Ghost"
  ["location"]=>
  string(29) "O2 Forum Kentish Town, London"
  ["event_date"]=>
  string(10) "2017-03-26"
  ["event_time"]=>
  string(8) "19:00:00"
  ["url"]=>
  string(1) "0"
  ["geo_lat"]=>
  string(18) "51.552197000000010"
  ["geo_long"]=>
  string(17) "-0.14196900000002"
  ["tags"]=>
  string(30) "rock,alternative,indie"
}
[3]=>
array(9) {
  ["id"]=>
  string(2) "10"
  ["title"]=>
  string(11) "Courteeners"
  ["location"]=>
  string(39) "Emirates Old Trafford, Lancashire C.C.C"
  ["event_date"]=>
  string(10) "2017-05-27"
  ["event_time"]=>
  string(8) "16:00:00"
  ["url"]=>
  string(1) "0"
  ["geo_lat"]=>
  string(18) "53.456428000000000"
  ["geo_long"]=>
  string(17) "-2.28679699999998"
  ["tags"]=>
  string(30) "rock,alternative,indie"
 }
}
["alternative"]=>
array(4) {
[0]=>
array(9) {
  ["id"]=>
  string(1) "1"
  ["title"]=>
  string(15) "Teleman Tickets"
  ["location"]=>
  string(20) "Concorde 2, Brighton"
  ["event_date"]=>
  string(10) "2017-02-20"
  ["event_time"]=>
  string(8) "20:00:00"
  ["url"]=>
  string(1) "0"
  ["geo_lat"]=>
  string(18) "50.817321799999990"
  ["geo_long"]=>
  string(17) "-0.12304610000001"
  ["tags"]=>
  string(30) "rock,alternative,indie"
}
[1]=>
array(9) {
  ["id"]=>
  string(1) "6"
  ["title"]=>
  string(6) "Sum 41"
  ["location"]=>
  string(23) "O2 Academy Leeds, Leeds"
  ["event_date"]=>
  string(10) "2017-02-26"
  ["event_time"]=>
  string(8) "19:00:00"
  ["url"]=>
  string(1) "0"
  ["geo_lat"]=>
  string(18) "53.802188400000000"
  ["geo_long"]=>
  string(17) "-1.54713770000001"
  ["tags"]=>
  string(21) "alternative,indie"
}
[2]=>
array(9) {
  ["id"]=>
  string(1) "8"
  ["title"]=>
  string(5) "Ghost"
  ["location"]=>
  string(29) "O2 Forum Kentish Town, London"
  ["event_date"]=>
  string(10) "2017-03-26"
  ["event_time"]=>
  string(8) "19:00:00"
  ["url"]=>
  string(1) "0"
  ["geo_lat"]=>
  string(18) "51.552197000000010"
  ["geo_long"]=>
  string(17) "-0.14196900000002"
  ["tags"]=>
  string(30) "rock,alternative,indie"
}
[3]=>
array(9) {
  ["id"]=>
  string(2) "10"
  ["title"]=>
  string(11) "Courteeners"
  ["location"]=>
  string(39) "Emirates Old Trafford, Lancashire C.C.C"
  ["event_date"]=>
  string(10) "2017-05-27"
  ["event_time"]=>
  string(8) "16:00:00"
  ["url"]=>
  string(1) "0"
  ["geo_lat"]=>
  string(18) "53.456428000000000"
  ["geo_long"]=>
  string(17) "-2.28679699999998"
  ["tags"]=>
  string(30) "rock,alternative,indie"
 }
}
["sax"]=>
array(1) {
[0]=>
array(9) {
  ["id"]=>
  string(1) "3"
  ["title"]=>
  string(9) "Take That"
  ["location"]=>
  string(22) "The SSE Hydro, Glasgow"
  ["event_date"]=>
  string(10) "2017-05-11"
  ["event_time"]=>
  string(8) "18:30:00"
  ["url"]=>
  string(1) "0"
  ["geo_lat"]=>
  string(18) "55.860156000000000"
  ["geo_long"]=>
  string(17) "-4.28525800000000"
  ["tags"]=>
  string(24) "pop,boy-band,sax"
 }
}
}
4
  • Can you please be more specific about which elements you're trying to remove? Commented May 24, 2017 at 21:00
  • removing duplicates doesn't make sense since these duplicates are in different categories (tags). Instead, you should suggest a new structure to store your data. Commented May 24, 2017 at 21:07
  • I want to keep the intersection values of [ROCK], [ALTERNATIVE] and [SAX] those are the keys with values of type array, some of those values are repeated in other keys Commented May 24, 2017 at 21:09
  • how can I divide the main array into three sub arrays, and get to use array_diff(), just an idea Commented May 24, 2017 at 21:10

2 Answers 2

2

I'm going to assume that if two bands have the same 'id' then they are identical

$unique_bands = array();
// if the master array is called bands    
foreach($bands as $genre){
    foreach($genre as $band){
        $unqiue_bands[$band['id']] = $band;
     }
}

Maybe put $unique_bands = array_values(array_filter($unique_bands)); at the end to reindex the array

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

1 Comment

That is what I am looking for thank you Jake mr. Genius ;)
0

You have a solution on the official documentation... A person made a simple function to do exactly what you want.

Example array:

<?php 
$details = array( 
    0 => array("id"=>"1", "name"=>"Mike",    "num"=>"9876543210"), 
    1 => array("id"=>"2", "name"=>"Carissa", "num"=>"08548596258"), 
    2 => array("id"=>"1", "name"=>"Mathew",  "num"=>"784581254"), 
); 
?>

Function:

<?php 
function unique_multidim_array($array, $key) { 
    $temp_array = array(); 
    $i = 0; 
    $key_array = array(); 

    foreach($array as $val) { 
        if (!in_array($val[$key], $key_array)) { 
            $key_array[$i] = $val[$key]; 
            $temp_array[$i] = $val; 
        } 
        $i++; 
    } 
    return $temp_array; 
} 
?> 

You will have to fix it to work your for array dimension.

6 Comments

I love this community when you give a solutions, maybe you are wrong, no one comments that and you have -100 downvotes without knowing why, but none of them gave a solution...
the id in my case is [rock], [alternative] and [sax] its coming from database
thank you anyways I really appreciate your help I have an answer up there! :) the solution is in the other answer
Hi matiaslauriti. I initially downvoted it based on revision 1, which really isn't even an answer. Since then you've copied and pasted code from the link, which is better, because at least it it's no longer link-only, but there's still no indication in your answer text as to what that code actually does.
@Don'tPanic thank you for your answer. You could have also written that so I would just give a better answer.
|

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.