katie hudson
katie hudson

Reputation: 2893

Obtaining unique values with different keys

after processing a CSV file I end up with a data structure like the following

Array
(
    [0] => Array
        (
            [Supplier Name] => John Lewis
            [Supplier Name2] => Fenwicks
            //other data
        )
    //Other elements
)

What I am trying to do is obtain all of the unique sluppliers. The problem is, if there is more than one supplier in the array, a number is added to the end of it e.g. Supplier Name2. What I have at the moment is this.

$suppliers = array();
foreach($csvArray as $k => $row) {
    foreach($row as $key => $value) {
        $suppliers[] = $csvArray[$k]['Supplier Name'];
    }
}
$uniqueSuppliers = array_unique($suppliers);

Now that returns all unique values where the key is Supplier Name. However, this does not take into affect Supplier Name2, 3, 4 etc. Is there any way I can include anything that starts with Supplier Name within this search?

Thanks

Upvotes: 0

Views: 30

Answers (1)

Barmar
Barmar

Reputation: 780798

You have the key in $key, just test if it begins with Supplier Name

foreach ($csvArray as $row) {
    foreach ($row as $key => $value) {
        if (strpos($key, 'Supplier Name') === 0) {
            $suppliers[] = $value;
        }
    }
}

Upvotes: 1

Related Questions