2

I have csv file, i am reading data from it, i have join all which are randomly matching their rows like brands, customer and soon..,

Here i want to remove all duplicate from multi dimensional array which already in array

Here you can see how all duplicate values are appending to array: image

Here is my php code

$csv = array_map('str_getcsv', file('test.csv'));
//echo '<pre>';
//print_r($csv);
//exit;
$brand =[];
foreach ($csv as $key => $value) {
    if(!(in_array($value[14], $brand))){
    $brand[$value[21]]['brands'][]=$value[14];    
    $brand[$value[21]]['products'][]=$value[1];    
    }   
}
echo '<pre>';
print_r($brand);

Thanks and welcome for all suggestions

2 Answers 2

1

You need to modify where you are looking in the in_array. You want to do this for both I'm sure, as well as check that it is set first:

foreach ($csv as $key => $value) {
    if(!isset($brand[$value[21]]['brands']) ||
       !in_array($value[14], $brand[$value[21]]['brands'])){
            $brand[$value[21]]['brands'][]=$value[14];
    }
    if(!isset($brand[$value[21]]['products']) ||
       !in_array($value[14], $brand[$value[21]]['products'])){
            $brand[$value[21]]['products'][]=$value[1];
    }            
}
Sign up to request clarification or add additional context in comments.

Comments

0

array_unique() could do that for you.

http://php.net/manual/en/function.array-unique.php

1 Comment

Can you please elaborate for the benefit of OP? Possibly provide example code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.