How to get unique value in multidimensional array
Seems pretty simple: extract all pid
values into their own array, run it through array_unique
:
$uniquePids = array_unique(array_map(function ($i) { return $i['pid']; }, $holder));
The same thing in longhand:
$pids = array();
foreach ($holder as $h) {
$pids[] = $h['pid'];
}
$uniquePids = array_unique($pids);
How to get unique value in multidimensional array PHP
if $mainArray = array(2)... , try this code:
$mainString = "";
foreach($mainArray as $array){
foreach($array as $row){
$mainString .= $row;
} $mainString .= ", ";
}
$tempArray = explode(", ", $mainString);
$finishArray = array_unique($tempArray);
How to use FILTER on a multidimensional array to retrieve unique values?
Use reduce
for getting the result.
var arr = [["apple","ghana",15],["apple","brazil",16],["orange","nigeria",10],["banana","bangladesh",20],["banana","ghana",6]];
const res = arr.reduce((a, c) => { if (!a.find(v => v[0] === c[0])) { a.push(c); } return a;}, []);
console.log(res);
.as-console-wrapper { min-height: 100% !important; top: 0;}
how to get array unique from multidimensional array when contains associative array?
You can use array_column
to convert the array into an associative array. Which will override duplicate values. Use array_values
to convert the associative array into a simple array.
$arr = //Your array
$result = array_values(array_column( $arr , null, 'product_code' ));
print_r( $result );
This will result to:
Array
(
[0] => Array
(
[product_code] => prod3
[qty] => 1
)
[1] => Array
(
[product_code] => prod4
[qty] => 7
)
)
Doc: array_column(), array_values()
php getting unique values of a multidimensional array
array_unique
is using string conversion before comparing the values to find the unique values:
Note: Two elements are considered equal if and only if
(string) $elem1 === (string) $elem2
. In words: when the string representation is the same. The first element will be used.
But an array will always convert to Array
:
var_dump("Array" === (string) array());
You can solve this by specifying the SORT_REGULAR mode in the second parameter of array_unique
:
$unique = array_unique($a, SORT_REGULAR);
Or, if that doesn’t work, by serializing the arrays before and unserializing it after calling array_unique
to find the unique values:
$unique = array_map('unserialize', array_unique(array_map('serialize', $a)));
How to get unique value in multidimensional array in Laravel?
$arr = [
"PM" => ["Zeb", "Pen", "Zeb", "Eds", "Fsa", "Zeb"],
"OS" => ["Min", "Kep", "Min"],
];
$cleanArray = collect($arr)->map(fn($subarr) => array_unique($subarr))->toArray();
will produce:
[
"PM" => ["Zeb", "Pen", "Eds", "Fsa"],
"OS" => ["Min", "Kep"]
]
group unique values in multidimensional array
Taking the note if the RestoreCode value exist only once put as it is with single model in array
into account, I think you might use a foreach loop and store the RestoreCode
of a single array as the key in the result array. Note that the result will differ from your wanted result as the item with a single RestoreCode will also be in the result.
Per iteration check if that key already exists. If if does not, add it and put the value for the Model in a new array.
If it does exist, add the values for TotalCharge
. To get the unique values for the Models, check if the current array for Model
already contains it.
If you want to reset the keys, you could use array_values.
$res = [];
foreach ($RestroreModels as $item) {
if (isset($res[$item["RestoreCode"]])) {
if (!in_array($item["Model"], $res[$item["RestoreCode"]]["Model"], true)) {
$res[$item["RestoreCode"]]["Model"][] = $item["Model"];
}
$res[$item["RestoreCode"]]["TotalCharge"] += $item["TotalCharge"];
} else {
$item["Model"] = [$item["Model"]];
$res[$item["RestoreCode"]] = $item;
}
}
print_r(array_values($res));
Php demo
How can you make a multidimensional array unique?
Since everyone given alternatives, here's a solution to the problem at-hand. Sometimes we have to work with the data we have, not re-arrange it the way we like it. That being said, this will remove all sub-sequent entries from the array that are duplicates.
$array = Array(
Array(
'name' => 'Test 3',
'slug' => 'test-3'
),
Array(
'name' => 'Foo',
'slug' => 'Bar'
),
Array(
'name' => 'Foo',
'slug' => 'Bar'
),
Array(
'name' => 'Test 1',
'slug' => 'test-1'
),
Array(
'name' => 'Test 2',
'slug' => 'test-2'
),
Array(
'name' => 'Test 3',
'slug' => 'test-3'
),
);
var_dump($array);
for ($e = 0; $e < count($array); $e++)
{
$duplicate = null;
for ($ee = $e+1; $ee < count($array); $ee++)
{
if (strcmp($array[$ee]['name'],$array[$e]['name']) === 0)
{
$duplicate = $ee;
break;
}
}
if (!is_null($duplicate))
array_splice($array,$duplicate,1);
}
var_dump($array);
Which will look like this:
array(6) {
[0]=>
array(2) {
["name"]=>
string(6) "Test 3"
["slug"]=>
string(6) "test-3"
}
[1]=>
array(2) {
["name"]=>
string(3) "Foo"
["slug"]=>
string(3) "Bar"
}
[2]=>
array(2) {
["name"]=>
string(3) "Foo"
["slug"]=>
string(3) "Bar"
}
[3]=>
array(2) {
["name"]=>
string(6) "Test 1"
["slug"]=>
string(6) "test-1"
}
[4]=>
array(2) {
["name"]=>
string(6) "Test 2"
["slug"]=>
string(6) "test-2"
}
[5]=>
array(2) {
["name"]=>
string(6) "Test 3"
["slug"]=>
string(6) "test-3"
}
}
array(4) {
[0]=>
array(2) {
["name"]=>
string(6) "Test 3"
["slug"]=>
string(6) "test-3"
}
[1]=>
array(2) {
["name"]=>
string(3) "Foo"
["slug"]=>
string(3) "Bar"
}
[2]=>
array(2) {
["name"]=>
string(6) "Test 1"
["slug"]=>
string(6) "test-1"
}
[3]=>
array(2) {
["name"]=>
string(6) "Test 2"
["slug"]=>
string(6) "test-2"
}
}
Related Topics
Set Session Variable Using JavaScript in PHP
Check If the Query Results Empty Row MySQLi
How to Show a Where Clause Just for One Field in MySQL
How to Add a New Method to a PHP Object on the Fly
How to Fix Memory Getting Exhausted with PHPexcel
How to Get Previous Month and Year Relative to Today, Using Strtotime and Date
How to Pass an Array via $_Get in PHP
Nginx Redirect Loop, Remove Index.PHP from Url
Binding Params for Pdo Statement Inside a Loop
Why am I Getting Mime-Type of .CSV File as "Application/Octet-Stream"
Fastest Way to Add Prefix to Array Keys
Best Methods to Clean Up a Hacked Site with No Clean Version Available
I Cannot Use MySQL_* Functions After Upgrading PHP
How to Re-Save the Entity as Another Row in Doctrine 2