herghost Posted April 6, 2012 Share Posted April 6, 2012 Hi guys I was wondering if anyone could point me in the right direction with this. I am using JSON to return the contents of a directory and then using a foreach loop to print each one to a new row in a table. Is there anyway to filter the results? Basically I just want to show the files that end in a .jar extension? Currently all sub directories and files are shown, however all the files I need to pull will be in the main directory. This is what I am using: <?php foreach ($installedplugins1['success'] as $v) { echo "<tr><td>".$v."</td>"; echo "<td><a href='index.php?dp=".$v."'>Disable Plugin</a></td>"; } ?> Any hints or reading material? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/260472-filtering-arrays-help/ Share on other sites More sharing options...
AyKay47 Posted April 6, 2012 Share Posted April 6, 2012 use glob for this. Quote Link to comment https://forums.phpfreaks.com/topic/260472-filtering-arrays-help/#findComment-1335050 Share on other sites More sharing options...
Psycho Posted April 6, 2012 Share Posted April 6, 2012 Where is this data coming from exactly? There may be better ways to get the data initially that would not require any post-processing. But, to answer your question, you can filter an array using the oddly named function array_filter(). Quote Link to comment https://forums.phpfreaks.com/topic/260472-filtering-arrays-help/#findComment-1335051 Share on other sites More sharing options...
Psycho Posted April 6, 2012 Share Posted April 6, 2012 use glob for this. @AyKay47: Note that glob() is case sensitive. There is a function that can convert a string into a pattern that will make glob() work in a case insensitive manner, but I cannot find it. EDIT: Found it sql_regcase() Quote Link to comment https://forums.phpfreaks.com/topic/260472-filtering-arrays-help/#findComment-1335055 Share on other sites More sharing options...
AyKay47 Posted April 6, 2012 Share Posted April 6, 2012 you can filter an array using the oddly named function array_filter(). heh... Psycho, my thoughts are that the OP stated he is grabbing files from a directory and looping through them to display them each in a new tbl row. I'm thinking he can use glob to grab all of the .jar files from the directory and foreach through them that way, something like this: ?> <table> <?php foreach(glob("/path/to/dir/*.jar") as $filename) { echo "<tr><td>".$filename."</td>"; echo "<td><a href='index.php?dp=".$filename."'>Disable Plugin</a></td></tr>"; } ?> </table> then again, I can only guess on the actual logic at this point. Quote Link to comment https://forums.phpfreaks.com/topic/260472-filtering-arrays-help/#findComment-1335056 Share on other sites More sharing options...
Psycho Posted April 6, 2012 Share Posted April 6, 2012 you can filter an array using the oddly named function array_filter(). heh... Psycho, my thoughts are that the OP stated he is grabbing files from a directory and looping through them to display them each in a new tbl row. I'm thinking he can use glob to grab all of the .jar files from the directory and foreach through them that way, something like this: Unless one of the files happens to be named .JAR, .Jar, etc. Then, using "*.jar" in the glob() function will not work. glob() doesn't accept true regex expressions, but you can build a case-insensitive expression using the function I posted above. Quote Link to comment https://forums.phpfreaks.com/topic/260472-filtering-arrays-help/#findComment-1335058 Share on other sites More sharing options...
AyKay47 Posted April 6, 2012 Share Posted April 6, 2012 you can filter an array using the oddly named function array_filter(). heh... Psycho, my thoughts are that the OP stated he is grabbing files from a directory and looping through them to display them each in a new tbl row. I'm thinking he can use glob to grab all of the .jar files from the directory and foreach through them that way, something like this: Unless one of the files happens to be named .JAR, .Jar, etc. Then, using "*.jar" in the glob() function will not work. glob() doesn't accept true regex expressions, but you can build a case-insensitive expression using the function I posted above. ah, just noticed the edit to your thread, yes that should work, any clue what the non-deprecated function is now, if one exists?. But again, until the OP responds, we can really only guess at the actual logic and if this will work in this case. Quote Link to comment https://forums.phpfreaks.com/topic/260472-filtering-arrays-help/#findComment-1335059 Share on other sites More sharing options...
herghost Posted April 6, 2012 Author Share Posted April 6, 2012 Hi Guys Thanks for all your responses, the data is coming from an API into the minecraft extension bukkit, my actual results from query display as: "result": "success", "source": "getDirectory", "success": ["plugins/Essentials.jar", "plugins/Essentials/", "plugins/Essentials/config.yml", "plugins/Essentials/items.csv", "plugins/Essentials/upgrades-done.yml", "plugins/Essentials/warps/", "plugins/Essentials/worth.yml", "plugins/JSONAPI.jar", "plugins/JSONAPI/", "plugins/JSONAPI/config.yml", "plugins/JSONAPI/config_rtk.yml", "plugins/JSONAPI/methods.json", "plugins/JSONAPI/methods/", "plugins/JSONAPI/methods/permissions.json", "plugins/JSONAPI/methods/readme.txt", "plugins/JSONAPI/methods/remotetoolkit.json", "plugins/JSONAPI/methods/system.json", "plugins/JSONAPI/methods/world.json", "plugins/MinecraftRKitPlugin.jar", "plugins/PluginMetrics/", "plugins/PluginMetrics/config.yml"] } So all I am trying to do is extract the results that end in .jar and ignore the rest so I can have a list of installed plugins. Quote Link to comment https://forums.phpfreaks.com/topic/260472-filtering-arrays-help/#findComment-1335060 Share on other sites More sharing options...
Psycho Posted April 6, 2012 Share Posted April 6, 2012 FYI: Here is a case-insensitive glob() solution to find ".jar" files $files = glob("/path/to/dir/*.".sql_regcase('jar')); BUt, to the OP's problem, since the data is created externally the array_filter() function is probably the correct solution. Quote Link to comment https://forums.phpfreaks.com/topic/260472-filtering-arrays-help/#findComment-1335061 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.