Jump to content

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


Tindrop
Go to solution Solved by Barand,

Recommended Posts

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.
 

Here is my code so far.
 

php: [select]

 

 $filterByArray2 = array ($truncFilter);
 
$filterByArray array_unique($filterByArray2SORT_REGULAR);
 
// sort($filterByArray);
 
 
echo '<pre>';
 
var_dump($filterByArray);
 echo 
'</pre>';

 

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.

Edited by Tindrop
Link to comment
Share on other sites

Hi Barand

 

Many thanks for your answer. 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 words (filtername) 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.

Edited by Tindrop
Link to comment
Share on other sites

  • Solution

Try something like

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

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

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

which gives

 $files Array
(
    [Edinburgh] => Array
        (
            [0] => file_A(Edinburgh).jpg
            [1] => file_B(Edinburgh).jpg
            [2] => file_D(Edinburgh).jpg
        )

    [Glasgow] => Array
        (
            [0] => file_C(Glasgow).jpg
        )

)

then

$dropdownVals = array_keys($files);

Now you have

 $dropdownVals Array
(
    [0] => Edinburgh
    [1] => Glasgow
)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.