Firstly, I realise this may appear as a duplicate as I have read a number of questions on a similar topic (1, 2) but I'm struggling to see how to re-architect the code base to fit my senario.
I am attempting to take an existing multi-dimensional array and remove any nodes that have a duplicate in a specific field. Here is dataset I am working with:
array(3) {
[0]=>
array(3) {
["company"]=>
string(9) "Company A"
["region"]=>
string(4) "EMEA"
["ctype"]=>
string(8) "Customer"
}
[1]=>
array(3) {
["company"]=>
string(9) "Company A"
["region"]=>
string(4) "EMEA"
["ctype"]=>
string(8) "Customer"
}
[2]=>
array(3) {
["company"]=>
string(9) "Company C"
["region"]=>
string(4) "EMEA"
["ctype"]=>
string(8) "Customer"
}
}
If this wasn't a multi-dimensional array would use in_array() to see if the dataset['company']
existed. If not I'd add it to my $unique
array, something like this:
$unique = array();
foreach ($dataset as $company) {
$company_name = $company['company'];
if ( !in_array($company_name, $unique) ) {
array_push($unique, $company_name);
}
}
var_dump($unique);
But I'm unsure how to traverse the muti-dimensional array to get to the ['company']
data to see if it exists (as it is the only item I need to check to see if it already exists).
I am looking to output exactly the same data as the initial dataset, just with the duplicate removed. Please can you point me in the right direction?