Get Removed/Duplicates Key values from Array PHP












1















First of all this is not a duplicate as i haven't found any info about this.
We can successfully remove duplicate values using the following from an array for example:



 $messages=   Array ( 
[0] => Array ( [user] => 2224 [sending_time] => 1536513903 [read_time] => 1536513941 [content] => sad [recipient_status] => read )
[1] => Array ( [user] => 3310 [sending_time] => 1536513903 [read_time] => 1536513941 [content] => sad [recipient_status] => read )
[2] => Array ( [user] => user1 [sending_time] => 1536513874 [read_time] => 1536567672 [content] => def [recipient_status] => read )
[3] => Array ( [user] => user1 [sending_time] => 1536513532 [read_time] => 1536513745 [content] => abc [recipient_status] => read )
)


Using the following i can remove the duplicates(by key):



        $MSGS_array  = array();
foreach ($messages as $message) {
$MSGS_array[$message['user']] = $message;
}


But the question is how can i get the removed user key value?



OR



How can i get all the duplicates having key user and their value(s) in an array from the array above?



Expected Output:



The output should only contain the removed/duplicates Like:(i just need the duplicates with they key user)



 $output=   Array (
[0] => Array ( [user] => user1 )
[1] => Array ( [user] => user1 )
);


OR



 $output=   Array (
[0] => Array ( [user] => user1 [sending_time] => 1536513874 [read_time] => 1536567672 [content] => def [recipient_status] => read )
[1] => Array ( [user] => user1 [sending_time] => 1536513532 [read_time] => 1536513745 [content] => abc [recipient_status] => read )
);









share|improve this question

























  • None of the entries in your example are exact duplicates of one of the others... what makes an entry a duplicate?

    – Nick
    Nov 25 '18 at 6:00











  • please check [3] and [4] array having value 'user1' similar.

    – MR_AMDEV
    Nov 25 '18 at 6:01











  • Please check the update.

    – MR_AMDEV
    Nov 25 '18 at 6:06
















1















First of all this is not a duplicate as i haven't found any info about this.
We can successfully remove duplicate values using the following from an array for example:



 $messages=   Array ( 
[0] => Array ( [user] => 2224 [sending_time] => 1536513903 [read_time] => 1536513941 [content] => sad [recipient_status] => read )
[1] => Array ( [user] => 3310 [sending_time] => 1536513903 [read_time] => 1536513941 [content] => sad [recipient_status] => read )
[2] => Array ( [user] => user1 [sending_time] => 1536513874 [read_time] => 1536567672 [content] => def [recipient_status] => read )
[3] => Array ( [user] => user1 [sending_time] => 1536513532 [read_time] => 1536513745 [content] => abc [recipient_status] => read )
)


Using the following i can remove the duplicates(by key):



        $MSGS_array  = array();
foreach ($messages as $message) {
$MSGS_array[$message['user']] = $message;
}


But the question is how can i get the removed user key value?



OR



How can i get all the duplicates having key user and their value(s) in an array from the array above?



Expected Output:



The output should only contain the removed/duplicates Like:(i just need the duplicates with they key user)



 $output=   Array (
[0] => Array ( [user] => user1 )
[1] => Array ( [user] => user1 )
);


OR



 $output=   Array (
[0] => Array ( [user] => user1 [sending_time] => 1536513874 [read_time] => 1536567672 [content] => def [recipient_status] => read )
[1] => Array ( [user] => user1 [sending_time] => 1536513532 [read_time] => 1536513745 [content] => abc [recipient_status] => read )
);









share|improve this question

























  • None of the entries in your example are exact duplicates of one of the others... what makes an entry a duplicate?

    – Nick
    Nov 25 '18 at 6:00











  • please check [3] and [4] array having value 'user1' similar.

    – MR_AMDEV
    Nov 25 '18 at 6:01











  • Please check the update.

    – MR_AMDEV
    Nov 25 '18 at 6:06














1












1








1


0






First of all this is not a duplicate as i haven't found any info about this.
We can successfully remove duplicate values using the following from an array for example:



 $messages=   Array ( 
[0] => Array ( [user] => 2224 [sending_time] => 1536513903 [read_time] => 1536513941 [content] => sad [recipient_status] => read )
[1] => Array ( [user] => 3310 [sending_time] => 1536513903 [read_time] => 1536513941 [content] => sad [recipient_status] => read )
[2] => Array ( [user] => user1 [sending_time] => 1536513874 [read_time] => 1536567672 [content] => def [recipient_status] => read )
[3] => Array ( [user] => user1 [sending_time] => 1536513532 [read_time] => 1536513745 [content] => abc [recipient_status] => read )
)


Using the following i can remove the duplicates(by key):



        $MSGS_array  = array();
foreach ($messages as $message) {
$MSGS_array[$message['user']] = $message;
}


But the question is how can i get the removed user key value?



OR



How can i get all the duplicates having key user and their value(s) in an array from the array above?



Expected Output:



The output should only contain the removed/duplicates Like:(i just need the duplicates with they key user)



 $output=   Array (
[0] => Array ( [user] => user1 )
[1] => Array ( [user] => user1 )
);


OR



 $output=   Array (
[0] => Array ( [user] => user1 [sending_time] => 1536513874 [read_time] => 1536567672 [content] => def [recipient_status] => read )
[1] => Array ( [user] => user1 [sending_time] => 1536513532 [read_time] => 1536513745 [content] => abc [recipient_status] => read )
);









share|improve this question
















First of all this is not a duplicate as i haven't found any info about this.
We can successfully remove duplicate values using the following from an array for example:



 $messages=   Array ( 
[0] => Array ( [user] => 2224 [sending_time] => 1536513903 [read_time] => 1536513941 [content] => sad [recipient_status] => read )
[1] => Array ( [user] => 3310 [sending_time] => 1536513903 [read_time] => 1536513941 [content] => sad [recipient_status] => read )
[2] => Array ( [user] => user1 [sending_time] => 1536513874 [read_time] => 1536567672 [content] => def [recipient_status] => read )
[3] => Array ( [user] => user1 [sending_time] => 1536513532 [read_time] => 1536513745 [content] => abc [recipient_status] => read )
)


Using the following i can remove the duplicates(by key):



        $MSGS_array  = array();
foreach ($messages as $message) {
$MSGS_array[$message['user']] = $message;
}


But the question is how can i get the removed user key value?



OR



How can i get all the duplicates having key user and their value(s) in an array from the array above?



Expected Output:



The output should only contain the removed/duplicates Like:(i just need the duplicates with they key user)



 $output=   Array (
[0] => Array ( [user] => user1 )
[1] => Array ( [user] => user1 )
);


OR



 $output=   Array (
[0] => Array ( [user] => user1 [sending_time] => 1536513874 [read_time] => 1536567672 [content] => def [recipient_status] => read )
[1] => Array ( [user] => user1 [sending_time] => 1536513532 [read_time] => 1536513745 [content] => abc [recipient_status] => read )
);






php arrays multidimensional-array






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 6:17







MR_AMDEV

















asked Nov 25 '18 at 5:51









MR_AMDEVMR_AMDEV

16611




16611













  • None of the entries in your example are exact duplicates of one of the others... what makes an entry a duplicate?

    – Nick
    Nov 25 '18 at 6:00











  • please check [3] and [4] array having value 'user1' similar.

    – MR_AMDEV
    Nov 25 '18 at 6:01











  • Please check the update.

    – MR_AMDEV
    Nov 25 '18 at 6:06



















  • None of the entries in your example are exact duplicates of one of the others... what makes an entry a duplicate?

    – Nick
    Nov 25 '18 at 6:00











  • please check [3] and [4] array having value 'user1' similar.

    – MR_AMDEV
    Nov 25 '18 at 6:01











  • Please check the update.

    – MR_AMDEV
    Nov 25 '18 at 6:06

















None of the entries in your example are exact duplicates of one of the others... what makes an entry a duplicate?

– Nick
Nov 25 '18 at 6:00





None of the entries in your example are exact duplicates of one of the others... what makes an entry a duplicate?

– Nick
Nov 25 '18 at 6:00













please check [3] and [4] array having value 'user1' similar.

– MR_AMDEV
Nov 25 '18 at 6:01





please check [3] and [4] array having value 'user1' similar.

– MR_AMDEV
Nov 25 '18 at 6:01













Please check the update.

– MR_AMDEV
Nov 25 '18 at 6:06





Please check the update.

– MR_AMDEV
Nov 25 '18 at 6:06












1 Answer
1






active

oldest

votes


















1














Here is one way to find the duplicate messages. First we find the non-unique users by checking the count of the values of user. Then we filter the messages in $messages by seeing if the user is in the non-unique users array:



$non_unique_users = array_filter(array_count_values(array_column($messages, 'user')), function ($v) { return $v != 1; });
$duplicate_messages = array_filter($messages, function ($v) use($non_unique_users) { return array_key_exists($v['user'], $non_unique_users); });
print_r($duplicate_messages);


Output:



Array ( 
[2] => Array ( [user] => user1 [sending_time] => 1536513874 [read_time] => 1536567672 [content] => def [recipient_status] => read )
[3] => Array ( [user] => user1 [sending_time] => 1536513532 [read_time] => 1536513745 [content] => abc [recipient_status] => read )
)


Demo on 3v4l.org



Update



To just return an array of the user keys, you can apply array_map to the $duplicate_messages array:



$duplicate_users = array_map(function ($v) { return array('user' => $v['user']); }, $duplicate_messages);


or you can derive it directly from the $messages and $non_unique_users arrays:



$duplicate_users = array_map(function ($v) use($non_unique_users) { if (array_key_exists($v['user'], $non_unique_users)) return array('user' => $v['user']); }, $duplicate_messages);


In both cases the output is



Array ( 
[2] => Array ( [user] => user1 )
[3] => Array ( [user] => user1 )
)


Demo on 3v4l.org






share|improve this answer


























  • Thanks this is working but how can i get the **user key and value only **without the rest sending_time,read_time etc.

    – MR_AMDEV
    Nov 25 '18 at 6:31











  • @MR_AMDEV see my edit. That should give you what you want.

    – Nick
    Nov 25 '18 at 7:32











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465016%2fget-removed-duplicates-key-values-from-array-php%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Here is one way to find the duplicate messages. First we find the non-unique users by checking the count of the values of user. Then we filter the messages in $messages by seeing if the user is in the non-unique users array:



$non_unique_users = array_filter(array_count_values(array_column($messages, 'user')), function ($v) { return $v != 1; });
$duplicate_messages = array_filter($messages, function ($v) use($non_unique_users) { return array_key_exists($v['user'], $non_unique_users); });
print_r($duplicate_messages);


Output:



Array ( 
[2] => Array ( [user] => user1 [sending_time] => 1536513874 [read_time] => 1536567672 [content] => def [recipient_status] => read )
[3] => Array ( [user] => user1 [sending_time] => 1536513532 [read_time] => 1536513745 [content] => abc [recipient_status] => read )
)


Demo on 3v4l.org



Update



To just return an array of the user keys, you can apply array_map to the $duplicate_messages array:



$duplicate_users = array_map(function ($v) { return array('user' => $v['user']); }, $duplicate_messages);


or you can derive it directly from the $messages and $non_unique_users arrays:



$duplicate_users = array_map(function ($v) use($non_unique_users) { if (array_key_exists($v['user'], $non_unique_users)) return array('user' => $v['user']); }, $duplicate_messages);


In both cases the output is



Array ( 
[2] => Array ( [user] => user1 )
[3] => Array ( [user] => user1 )
)


Demo on 3v4l.org






share|improve this answer


























  • Thanks this is working but how can i get the **user key and value only **without the rest sending_time,read_time etc.

    – MR_AMDEV
    Nov 25 '18 at 6:31











  • @MR_AMDEV see my edit. That should give you what you want.

    – Nick
    Nov 25 '18 at 7:32
















1














Here is one way to find the duplicate messages. First we find the non-unique users by checking the count of the values of user. Then we filter the messages in $messages by seeing if the user is in the non-unique users array:



$non_unique_users = array_filter(array_count_values(array_column($messages, 'user')), function ($v) { return $v != 1; });
$duplicate_messages = array_filter($messages, function ($v) use($non_unique_users) { return array_key_exists($v['user'], $non_unique_users); });
print_r($duplicate_messages);


Output:



Array ( 
[2] => Array ( [user] => user1 [sending_time] => 1536513874 [read_time] => 1536567672 [content] => def [recipient_status] => read )
[3] => Array ( [user] => user1 [sending_time] => 1536513532 [read_time] => 1536513745 [content] => abc [recipient_status] => read )
)


Demo on 3v4l.org



Update



To just return an array of the user keys, you can apply array_map to the $duplicate_messages array:



$duplicate_users = array_map(function ($v) { return array('user' => $v['user']); }, $duplicate_messages);


or you can derive it directly from the $messages and $non_unique_users arrays:



$duplicate_users = array_map(function ($v) use($non_unique_users) { if (array_key_exists($v['user'], $non_unique_users)) return array('user' => $v['user']); }, $duplicate_messages);


In both cases the output is



Array ( 
[2] => Array ( [user] => user1 )
[3] => Array ( [user] => user1 )
)


Demo on 3v4l.org






share|improve this answer


























  • Thanks this is working but how can i get the **user key and value only **without the rest sending_time,read_time etc.

    – MR_AMDEV
    Nov 25 '18 at 6:31











  • @MR_AMDEV see my edit. That should give you what you want.

    – Nick
    Nov 25 '18 at 7:32














1












1








1







Here is one way to find the duplicate messages. First we find the non-unique users by checking the count of the values of user. Then we filter the messages in $messages by seeing if the user is in the non-unique users array:



$non_unique_users = array_filter(array_count_values(array_column($messages, 'user')), function ($v) { return $v != 1; });
$duplicate_messages = array_filter($messages, function ($v) use($non_unique_users) { return array_key_exists($v['user'], $non_unique_users); });
print_r($duplicate_messages);


Output:



Array ( 
[2] => Array ( [user] => user1 [sending_time] => 1536513874 [read_time] => 1536567672 [content] => def [recipient_status] => read )
[3] => Array ( [user] => user1 [sending_time] => 1536513532 [read_time] => 1536513745 [content] => abc [recipient_status] => read )
)


Demo on 3v4l.org



Update



To just return an array of the user keys, you can apply array_map to the $duplicate_messages array:



$duplicate_users = array_map(function ($v) { return array('user' => $v['user']); }, $duplicate_messages);


or you can derive it directly from the $messages and $non_unique_users arrays:



$duplicate_users = array_map(function ($v) use($non_unique_users) { if (array_key_exists($v['user'], $non_unique_users)) return array('user' => $v['user']); }, $duplicate_messages);


In both cases the output is



Array ( 
[2] => Array ( [user] => user1 )
[3] => Array ( [user] => user1 )
)


Demo on 3v4l.org






share|improve this answer















Here is one way to find the duplicate messages. First we find the non-unique users by checking the count of the values of user. Then we filter the messages in $messages by seeing if the user is in the non-unique users array:



$non_unique_users = array_filter(array_count_values(array_column($messages, 'user')), function ($v) { return $v != 1; });
$duplicate_messages = array_filter($messages, function ($v) use($non_unique_users) { return array_key_exists($v['user'], $non_unique_users); });
print_r($duplicate_messages);


Output:



Array ( 
[2] => Array ( [user] => user1 [sending_time] => 1536513874 [read_time] => 1536567672 [content] => def [recipient_status] => read )
[3] => Array ( [user] => user1 [sending_time] => 1536513532 [read_time] => 1536513745 [content] => abc [recipient_status] => read )
)


Demo on 3v4l.org



Update



To just return an array of the user keys, you can apply array_map to the $duplicate_messages array:



$duplicate_users = array_map(function ($v) { return array('user' => $v['user']); }, $duplicate_messages);


or you can derive it directly from the $messages and $non_unique_users arrays:



$duplicate_users = array_map(function ($v) use($non_unique_users) { if (array_key_exists($v['user'], $non_unique_users)) return array('user' => $v['user']); }, $duplicate_messages);


In both cases the output is



Array ( 
[2] => Array ( [user] => user1 )
[3] => Array ( [user] => user1 )
)


Demo on 3v4l.org







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 7:32

























answered Nov 25 '18 at 6:27









NickNick

34.9k132143




34.9k132143













  • Thanks this is working but how can i get the **user key and value only **without the rest sending_time,read_time etc.

    – MR_AMDEV
    Nov 25 '18 at 6:31











  • @MR_AMDEV see my edit. That should give you what you want.

    – Nick
    Nov 25 '18 at 7:32



















  • Thanks this is working but how can i get the **user key and value only **without the rest sending_time,read_time etc.

    – MR_AMDEV
    Nov 25 '18 at 6:31











  • @MR_AMDEV see my edit. That should give you what you want.

    – Nick
    Nov 25 '18 at 7:32

















Thanks this is working but how can i get the **user key and value only **without the rest sending_time,read_time etc.

– MR_AMDEV
Nov 25 '18 at 6:31





Thanks this is working but how can i get the **user key and value only **without the rest sending_time,read_time etc.

– MR_AMDEV
Nov 25 '18 at 6:31













@MR_AMDEV see my edit. That should give you what you want.

– Nick
Nov 25 '18 at 7:32





@MR_AMDEV see my edit. That should give you what you want.

– Nick
Nov 25 '18 at 7:32




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465016%2fget-removed-duplicates-key-values-from-array-php%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen