Help with this array to get unique values in foreach loop please?

Hi I am new this forum and looking for some much appreciated help with something that has me stumped.

I have extracted regions from image names in a directory.

example: ‘edinburgh_castle_(Edinburgh).jpg’ (Extracted Edinburgh from in-between the brackets)

So I have a list of Regions, extracted from all the various image names in the directory which I want to populate into a dynamic drop down menu for filtering purposes.

Here’s my issue…

I want there to be only one ‘Edinburgh’ option appearing in my drop down menu. I don’t want duplicates.

[hr]

Here is my code so far.

[php]$filterByArray2 = array ($truncFilter);
$filterByArray = array_unique($filterByArray2, SORT_REGULAR);
// sort($filterByArray);

echo ‘

’;
var_dump($filterByArray);
echo ‘
’;[/php]

[hr]

Produces Result…
array(1) {[0]=>string( 7 ) “Glasgow”}
array(1) {[0]=>string( 8 ) “Edinburgh”}
array(1) {[0]=>string( 8 ) “Edinburgh”}
array(1) {[0]=>string( 8 ) “Edinburgh”}

A for each loop…
foreach ($filterByArray as $key => $value) {echo “$key $value,”;}

prints out… 0 Glasgow,0 Edinburgh,0 Edinburgh,0 Edinburgh,

Can anyone, better than myself, tell me how to return ‘Glasgow,Edinburgh’ with no duplicates. I’m not sure how to do it when all the $keys are 0?

Many thanks in advance.

PS: I think I have posted this in the wrong category. Can someone move it for me please?

Look at array-unique

Many thanks for your reply. Much appreciated!

If I was manually creating the array I would know to do an array containing all the Regions I could think of. And do it the way you have suggested.

In this case I have scanned (glob) all the image names in a folder that have been named in a certain way (naming protocol) i.e ‘some_named_image_(filtername).jpg’

In my code (part that you don’t see) I have created a list by extracting all the various filtername words that lie between the brackets. (in my code this filtername list is the variable ‘$truncFilter’) This is because I want to then create another list of unique values to populate a dynamic dropdown, namely a list with all the filternames included, just once, for each filtername. I have managed to do that but the filternames are including multiple duplicate values depending on how many matching filternames in the image names have been entered between the brackets of all the image names. In this case there are four images. 1 with Glasgow extracted between the brackets and 3 with, for example purposes, Edinburgh. I want to find a way to have just 1 Glasgow and 1 Edinburgh, and 1 of whatever else is found, in my drop down list to use as a filter. Stripping out all duplicates.

If you look at my code I tried using array_unique() but it has no effect. I can’t enter the values of the array manually as I don’t know what all the filtername values are going to be which have been extracted from the image names. I don’t have a pre-conceived list of what filtername values are going to be found in each image name.

Hopefully I have explained it ok.

This is the output:

array(2) { [0]=> string(9) “Edinburgh” [1]=> string(7) “Glasgow” }

For this code:

[php]<?php

$truncFilter = array(“Edinburgh”, “Glasgow”, “Edinburgh”,“Edinburgh”,“Edinburgh”);

$filterByArray = array_unique($truncFilter);
$truncFilter = sort($filterByArray);

var_dump($filterByArray);[/php]

Granted, I had to create the array first.

Thanks astonecipher

Yes that works, but the trouble is I don’t always know what is going to be entered within the (brackets) by the person who uploads the image. Or what topic/category the filternames are going to be.

I’m trying to automate the process by gathering the values in the script and populating the filter dropdown. As i say I can do it just can’t make them all appear only once in the dropdown option list.

I know I don’t make life easy for myself but would be of great use if there was a way to do it.

Cheers

I don’t know the sample input you are using, but if the split is using the same location and that is pulling the same information (the city) array-unique will filter the data so no duplicates appear. But that assumes the same format.

What you could do is create your own filename for the uploads to ensure they meet the format you are expecting. I have used that on many projects.

I’ll upload the full script and show a link to a page to show it , nearly, working. A really nice little script if can get it to work.

Hi astonecipher

I thought it only right to reply and show you the solution which works and I, admittedly, got help with.

New code for dropdown with unique values START

$dir = array (
‘file_A(Edinburgh).jpg’,
‘file_B(Edinburgh).jpg’,
‘file_C(Glasgow).jpg’,
‘file_D(Edinburgh).jpg’
);

function getFilterVal($fname)
{
$p1 = strpos($fname,’[’);
$p2 = strrpos($fname,’]’);
return substr($fname, $p1+1, $p2-$p1-1);
}

// PROCESS THE FILES AND STORE IN 2D ARRAY BY FILTER VALUE
$files = array();
foreach ($dir as $fname) {$filter = getFilterVal($fname);$files[$filter][] = $fname;}

$dropdownVals = array_keys($files);

$select = ‘’;
$select .= ‘’;
$select .= ‘Filter’;
foreach ($dropdownVals as $key => $value)
{
($_GET[‘filter_T’] == $value) ? $sticky = ’ selected=“selected”’ : $sticky = ‘’;
$select .= ‘<option value="’. $self .’/’.$value.’"’.$sticky.’>’.$value.’’;
}
$select .= ‘’;

$select .= '<input type="button" onClick="location=document.jump.menu.options[document.jump.menu.selectedIndex].value;" value="SORT">'; 
$select .= '</form>';

echo “


”;
echo $select; // print dropdown
echo “
”;

New code for dropdown with unique values END

// $select variable now prints out all the values found between the brackets only once

There is always more than one way to write a piece of code and I’m glad you got it to work. But, I should point out that in the specific example,

[php] $p1 = strpos($fname,’[’);
$p2 = strrpos($fname,’]’);[/php]

Will return false on the file names.

'file_A(Edinburgh).jpg',
'file_B(Edinburgh).jpg',
'file_C(Glasgow).jpg',
'file_D(Edinburgh).jpg'

Thanks, all I’m wanting, in this case, is the unique values for the list menu. I am dealing with the filenames with other code in the script.

Got it working fine! Appreciate your time. - Great forum!!!

Sponsor our Newsletter | Privacy Policy | Terms of Service