Tindrop Posted October 11, 2014 Share Posted October 11, 2014 (edited) 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($filterByArray2, SORT_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 October 11, 2014 by Tindrop Quote Link to comment https://forums.phpfreaks.com/topic/291574-help-with-this-array-to-get-unique-values-in-foreach-loop-please/ Share on other sites More sharing options...
Barand Posted October 11, 2014 Share Posted October 11, 2014 (edited) Just create a simple single dimension array then use array_unique. You are creating an array containing several one-item arrays $arr = array ('Glasgow', 'Edinburgh', 'Edinburgh', 'Edinburgh'); $uniq = array_unique($arr); Edited October 11, 2014 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/291574-help-with-this-array-to-get-unique-values-in-foreach-loop-please/#findComment-1493334 Share on other sites More sharing options...
Tindrop Posted October 11, 2014 Author Share Posted October 11, 2014 (edited) 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 October 11, 2014 by Tindrop Quote Link to comment https://forums.phpfreaks.com/topic/291574-help-with-this-array-to-get-unique-values-in-foreach-loop-please/#findComment-1493355 Share on other sites More sharing options...
Solution Barand Posted October 12, 2014 Solution Share Posted October 12, 2014 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/291574-help-with-this-array-to-get-unique-values-in-foreach-loop-please/#findComment-1493378 Share on other sites More sharing options...
Tindrop Posted October 12, 2014 Author Share Posted October 12, 2014 Barand I can't thank you enough. Works perfectly! D Quote Link to comment https://forums.phpfreaks.com/topic/291574-help-with-this-array-to-get-unique-values-in-foreach-loop-please/#findComment-1493417 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.