note 116302 added to function.array-unique
| From: | (anshkatriyaatgmail) | Date: | Tue, 09 Dec 2014 10:15:39 +0000 |
| Subject: | note 116302 added to function.array-unique | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-201095@lists.php.net to get a copy of this message | ||
Create multidimensional array unique for any single key index.
e.g I want to create multi dimentional unique array for specific code
Code :
My array is like this,
<?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"),
);
?>
you can make it unique for any field like id, name or num.
I have develop this function for same :
<?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;
}
?>
Now, call this function anywhere from your code,
something like this,
<?php
$details = unique_multidim_array($details,'id');
?>
Output will be like this :
<?php
$details = array(
[0]=>array("id"=>"1","name"=>"Mike","num"=>"9876543210"),
[1]=>array("id"=>"2","name"=>"Carissa","num"=>"08548596258"),
);
?>
----
Server IP: 72.52.91.14
Probable Submitter: 202.131.119.122
----
Manual Page -- http://php.net/manual/en/function.array-unique.php
Edit -- https://master.php.net/note/edit/116302
Del: integrated -- https://master.php.net/note/delete/116302/integrated
Del: useless -- https://master.php.net/note/delete/116302/useless
Del: bad code -- https://master.php.net/note/delete/116302/bad+code
Del: spam -- https://master.php.net/note/delete/116302/spam
Del: non-english -- https://master.php.net/note/delete/116302/non-english
Del: in docs -- https://master.php.net/note/delete/116302/in+docs
Del: other reasons-- https://master.php.net/note/delete/116302
Reject -- https://master.php.net/note/reject/116302
Search -- https://master.php.net/manage/user-notes.php