Skip to content

Instantly share code, notes, and snippets.

@RetiredQQ
Created December 1, 2022 05:11
Show Gist options
  • Save RetiredQQ/1299d1a7132a106f8bd6c76e59a1dad6 to your computer and use it in GitHub Desktop.
Save RetiredQQ/1299d1a7132a106f8bd6c76e59a1dad6 to your computer and use it in GitHub Desktop.
Create multidimensional array unique for any single key index.
<?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