Created
December 1, 2022 05:11
-
-
Save RetiredQQ/1299d1a7132a106f8bd6c76e59a1dad6 to your computer and use it in GitHub Desktop.
Create multidimensional array unique for any single key index.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// https://www.php.net/manual/en/function.array-unique.php#116302 | |
$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"), | |
); | |
$details = array_unique_multidimesional($details,'id'); | |
function array_unique_multidimesional($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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment