/    Sign up×
Community /Pin to ProfileBookmark

Sort a PHP array order using contents of a file such as date.php?

Hi, I have a PHP foreach array obtaining each folder inside a ‘files’ folder. By having *YYYYMMDD* date numbers at the beginning of each folder the array is obtaining and by using arrayreverse, I can have the array output the folders with the newest always at the top.

That works fine, but if I want to change a date, that means changing the folder structure, so I would have to do redirects each time I do that.

Instead of having the YYYYMMDD in the folder names which the array is currently ordered by, is there any way I can use a $date variable assigned to get_file_contents to use YYYY-MM-DD written in a date.php file to organise the array?

Here’s my current php script:

<?php
foreach(array_reverse(glob(‘files/*’, GLOB_ONLYDIR)) as $dir) {
$dir = str_replace(‘files/’, ”, $dir);
?>
<!– html –>
<?php
}
?>

Now if I could do something in the lines of the below, that’d be great.

$date = file_get_contents(“$dir/includes/date.php”);
$sorted_dir = $dir;
rsort($dir by $date) as $sorted_dir;

I know that “`rsort($dir by $date) as $sorted_dir;“` likely isn’t how you’d do it. Just written that as hopefully to give an idea of what I’m looking for.

[My last thread here](https://www.webdeveloper.com/forum/d/368614-ignorehide-yyyymmdd-from-folder-names-in-urls-no-cms), I was trying to find out some sort of method with htaccess to to either mask the *YYYYMMDD* numbers out of the folder name in the url, or if the numbers are incorrect, correct them using the rest of the folder name after the numbers.
I thought that would I could do with that, have YYYYMMDD as parent folders instead, so I can then just hide them in the url. But it seems like you cannot do that as they’ll conflict.

So even though it’s linked this to my last thread, seperating as it is quite a different method I’m asking here.

If I can do in anyway this with sorting the array using the date.php, then that’ll be great.

Look forward to hearing back.

to post a comment
PHP

36 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJun 24.2018 — A rough approach: Create a second array where date is the key; then sort this array by key:
// initial array containing the dirs
$dirs = glob('files/*', GLOB_ONLYDIR);
// new array with date as key
$dirs_date = [];
foreach ($dirs as $cdir) {
// read date from file
$date = file_get_contents("$cdir/includes/date.php");
// add current dir to new array where date is the key
$dirs_date[$date] = $cdir;
}
// now we sort the new array
ksort($dirs_date);
Untested, I didn't have time to create an appropriate structure.
Copy linkTweet thisAlerts:
@jasewolfauthorJun 24.2018 — @Sempervivum#1593250 Right I'm putting this below, my html for each item, placing ``$dirs_date`</C> into the path. However it's just turning up an as "Array" instead of the path for each item. If I place <C>`$cdir`` in instead, it shows the path, but it will only show one not all.

<div class="download-menu-item">
<a href="<?=$dirs_date?>">
<img class="download-menu-image" src="<?=$dirs_date?>/images/main/thumbs/image-small-thumb.jpg"/>
<span class="download-menu-title">
<?php include("$dirs_date/includes/title.php"); ?>
</span>
</a>
</div>


Do appreciate this though, definitely looks like are on the right tracks.
Copy linkTweet thisAlerts:
@SempervivumJun 24.2018 — You are right, $dirs_date is an array and it cannot be used the way you do. However I do not know what you intend to do. Do you want to get the latest directory and use it for your HTML?
Copy linkTweet thisAlerts:
@jasewolfauthorJun 24.2018 — @Sempervivum#1593254

What I want to do is create a list of the HTML for each directory from the array, so it shows up as a menu, and is ordered via using the date.php instead of the folder names so I can eliminate the date from the url path so I can change the date without the url changing. Here's the original working working code that that is ordered by the folder names.

<?php
foreach(array_reverse(glob('files/*', GLOB_ONLYDIR)) as $dir) {
$dir = str_replace('files/', '', $dir);
?>

<div class="download-menu-item">
<a href="files/<?=$dir?>">
<img class="download-menu-image" src="files/<?=$dir?>/images/main/thumbs/image-small-thumb.jpg"/>
<span class="download-menu-title">
<?php include("files/$dir/includes/title.php"); ?>
</span>
</a>
</div>

<?php
}
?>
Copy linkTweet thisAlerts:
@SempervivumJun 24.2018 — I see! You just need to pick the current item from the array and use it in the HTML:
&lt;?php
foreach($dirs_date as $key=&gt;$dir) { // this line changed, getting the current dir from the new array
$dir = str_replace('files/', '', $dir);
?&gt;

&lt;div class="download-menu-item"&gt;
&lt;a href="files/&lt;?=$dir?&gt;"&gt;
&lt;img class="download-menu-image" src="files/&lt;?=$dir?&gt;/images/main/thumbs/image-small-thumb.jpg"/&gt;
&lt;span class="download-menu-title"&gt;
&lt;?php include("files/$dir/includes/title.php"); ?&gt;
&lt;/span&gt;
&lt;/a&gt;
&lt;/div&gt;

&lt;?php
}
?&gt;
Copy linkTweet thisAlerts:
@jasewolfauthorJun 24.2018 — @Sempervivum#1593256 So I insert that below the code you did I assume?
Copy linkTweet thisAlerts:
@SempervivumJun 24.2018 — Yes, that array needs to be created first, thus you have to place the code above.
Copy linkTweet thisAlerts:
@jasewolfauthorJun 24.2018 — @Sempervivum#1593258 You are a star! Changed ksort to krsort for descending order! THANK YOU!!! Now I can eliminate the dates from the url ?

[Here's it in action](https://mods.jasewolf.com) (the all downloads menu). It's duplicated due to I duplicated my one download to test. See the links when you hover. test122 is set to 2016-01-19, the actual one is set to 2017-01-19 and test211 is set to 2018-01-19. Great to see that working!
Copy linkTweet thisAlerts:
@SempervivumJun 24.2018 — Agree, great that it's working!
Copy linkTweet thisAlerts:
@jasewolfauthorJun 24.2018 — @Sempervivum#1593260 Only has taken me ages to get here xD. But better late than never haha. Wish Ithought of it sooner getting this done from the php rather than trying to do crazy tthings with htaccess to either hide or ignore the numbers in the url which is far too complicating for what I needed. Again thanks!
Copy linkTweet thisAlerts:
@jasewolfauthorJun 25.2018 — Now to if possible take things a step further, how would I go about adding more info to the file I'm obtaining the contents of?

So say I want to organise it like this:
``<i>
</i>Arraysortdate: "2018-06-25"
Month: "June 2018"
Category: "cat1", "cat2"…etc
Tags: "tag1", "tag2", "tag3"…etc<i>
</i>
``

How would I do the following:

  • * Get the krsort to use the 'Arraysortdate' for ordering. (Though that actually shouldn't be an issue, as it's staying the same on everything up until the numbers. So that won't need any changes I'm guessing.)


  • * Make arrays on month pages use 'Month' to limit to the array to only that month of the year.


  • * Do the same but for tags and categories on tag/category pages.


  • * Lastly, I'd like to have a recent only array that only obtains say the first 5 folders.


  • I am not asking for all these different things to get read all at once of course. I will be having the seperate pages for each. But whilst I assume that the month won't be an issue, for the tags/categories, would I need to do them on seperate lines?

    I'm assuming the sort of thing I'm looking for is to check if a value exists or not in that file before putting it in the array?

    If I can do something like this, that'd be wicked. I'm not so much worried with this for my downloads site as such because I am not going to put so much on that it's needed. But if I can do this, then that'll be useful for moving away from Wordpress to flat-file that's 100% integrated into my site code, not just intergrated by looks.
    Copy linkTweet thisAlerts:
    @SempervivumJun 25.2018 — If this file is managed by an experienced person, it would be handy for the coding it the content is in JSON format like this:
    {
    Arraysortdate: "2018-06-25",
    Month: "June 2018",
    Category: ["cat1", "cat2"],
    Tags: ["tag1", "tag2", "tag3"]
    }
    Then you could read this information in two lines:
    $string = file_get_contents(""$cdir/includes/date.json"");
    $json_a = json_decode($string, true);
    Copy linkTweet thisAlerts:
    @SempervivumJun 25.2018 — Then add the information to the additional array like this:
    // initial array containing the dirs
    $dirs = glob('files/*', GLOB_ONLYDIR);
    // new array with date as key
    $dirinfo_arr = [];
    foreach ($dirs as $cdir) {
    // read date from file
    $dirinfo_str = file_get_contents("$cdir/includes/date.php");
    $dirinfo = json_decode($dirinfo_str, TRUE);
    // add current directory to the info array
    $dirinfo['dir'] = $cdir;
    // add current dir to new array where date is the key
    $dirinfo_arr[$dirinfo['Arraysortdate']] = $cdir;
    }
    // now we sort the new array
    ksort($dirinfo_arr);

    Then the array can be filtered additionally by, e. g. month, by use of array_filter():

    http://php.net/manual/de/function.array-filter.php
    Copy linkTweet thisAlerts:
    @jasewolfauthorJun 25.2018 — @Sempervivum#1593269 Thanks I'll get to trying that soon once I've got my things setup to test.

    For my archives, I am putting the monthly archives in this structure ``archives/2018/05/`</C> with <C>`includes/title.php`</C> storing the name for each month page.

    I am using my original script to generate a monthly archive link list on the side of the page, that all is working by just changing the glob path to have another <C>
    `/*`</C> at the end as well as change <C>`GLOB_ONLYDIR`</C> to <C>`GLOB_BRACE`</C>.

    However as I am including the title.php into the generated link text, I am having errors throw up for the year directories which don't have a <C>
    `includes/title.php``
    .

    So what I need there is to exclude the year directories from being put into the array, only the month directories inside of the year directories. As it'll just endlessly make error logs otherwise lol.
    Copy linkTweet thisAlerts:
    @SempervivumJun 25.2018 — I'm a bit confused: Is this a different subject and script or does your explanation refer to the script we discussed before?

    Anyway, a solution might be to check the return value of file_get_contents() which is FALSE in case of error or if the file doesn't exist.
    Copy linkTweet thisAlerts:
    @jasewolfauthorJun 25.2018 — @Sempervivum#1593292 Sorry for the confusion. It's the same script as in the original post that orders by name being used in this case for a link list of all the month pages.

    Only changes are that I'm putting the glob path with ``/*/*`</C> instead of just one <C>`/*`</C>, along with changing <C>`GLOB_ONLYDIR`</C> to <C>`GLOB_BRACE`</C> to grab all folders.

    The php script has no error itself here. It's the includes that are doing that.

    But if I can set say the first <C>
    `/*`</C> to <C>`$year`</C> and the second to <C>`$month`</C>, so in the include path, I can put <C>`../$year/$month/..``
    , that'll mean it'll be not using the year folders.

    Currently got errors for that on hide though so it doesn't bother until sort it so no rush.
    Copy linkTweet thisAlerts:
    @jasewolfauthorJun 26.2018 — Been testing out that code, but it only loads the first folder unless I remove ``$dirinfo['Arraysortdate']`</C>, then it'll load the rest but not ordered due to not having anything to order with.
    <CODE>
    `<i>
    </i>&lt;?php
    // initial array containing the dirs
    $dirs = glob('blog/posts/*', GLOB_ONLYDIR);
    // new array with date as key
    $dirinfo_arr = [];
    foreach ($dirs as $cdir) {
    // read date from file
    $dirinfo_str = file_get_contents("$cdir/includes/post-info.json");
    $dirinfo = json_decode($dirinfo_str, TRUE);
    // add current directory to the info array
    $dirinfo['dir'] = $cdir;
    // add current dir to new array where date is the key
    $dirinfo_arr[$dirinfo['Arraysortdate']] = $cdir;
    }
    // now we sort the new array
    krsort($dirinfo_arr);

    foreach($dirinfo_arr as $key=&gt;$dir) { // this line changed, getting the current dir from the new array
    ?&gt;
    &lt;!--HTML--&gt;
    &lt;?php
    }
    ?&gt;<i>
    </i>
    `</CODE>
    Also, I do, like in my last response, need to figure out getting GlOB_BRACE to ignore current the parent directories when I here (as I have 2 blogs) do <C>
    `$dirs = glob('{blog1/posts/*,blog2/posts/*}', GLOB_BRACE);``
    as the result is putting the blog1&2 & posts directories into the array when I just want the directories inside of posts. So I need to have a rule or something to include the 3rd level sub directories in the array.
    Copy linkTweet thisAlerts:
    @rootJun 26.2018 — Seriously...USE A DATABASE, EVEN IF its only a temporary table to do this sort of compiling and sorting, it will take less code and quicker, easier searching for that data you want or don't want...

    You will learn a term, it is called KISS and it basically starts out like Keep It Simple and often ends as for Stupid or has been seen as Silly and also in the name of Sanity which I think applies in this case, Keeping It Simple for you Sanity (and others) is a lesson to take home here.

    There comes a time, all programmers know this that are well seasoned, oiled and otherwise lubricated will know when to quit and start from a fresh angle. Some of the best software ideas started out and ended up being rewritten because things spiralled out of control.

    Its like in music, musicians will argue that if you can not say what you need to say in a tune in 3mins and 40 seconds, then you should get out the game. IDK about that but the idea is similar in as far as, writing code has always been about the art of squeezing the most out of your system, with the least input in to it but in a secure and safe manner...

    Seriously... Get your file information scraped in to a database along with the associated data you want and then query the database and see the results.
    Copy linkTweet thisAlerts:
    @SempervivumJun 26.2018 — As before I didn't test my code before posting and the result was that there was a serious error inside:

    The last line in the loop has to read:
    $dirinfo_arr[$dirinfo['Arraysortdate']] = $dirinfo;
    instead of
    $dirinfo_arr[$dirinfo['Arraysortdate']] = $cdir;
    I tested this now and it works fine for me.
    Copy linkTweet thisAlerts:
    @jasewolfauthorJun 26.2018 — @Sempervivum#1593330 That's returning as "Array" now instead. Do I need to change anything in the foreach before the html?
    Copy linkTweet thisAlerts:
    @SempervivumJun 26.2018 — I forgot to mention that my editor told me to double quote the field names in the json, like so:
    {
    "Arraysortdate": "2018-07-27",
    "Month": "June 2018",
    "Category": ["cat1", "cat2"],
    "Tags": ["tag1", "tag2", "tag3"]
    }

    That's returning as "Array"[/quote]I assume that this is correct - $dirinfo_arr is an array of two dimension s now. Try this for the second loop:
    foreach ($dirinfo_arr as $key =&gt; $dir) { // this line changed, getting the current dir from the new array
    ?&gt;
    &lt;!--HTML--&gt;
    directory is: &lt;?php echo $dir['dir']; ?&gt;
    &lt;?php
    }
    ?&gt;
    Copy linkTweet thisAlerts:
    @jasewolfauthorJun 26.2018 — That's sorted it.. once I also removed the last comma due to hiding the tag line for now in the json.

    That's done, now to get figuring out the GLOB_BRACE issue as I stated about above and then as well get figuring out the filtering.
    Copy linkTweet thisAlerts:
    @SempervivumJun 26.2018 — Fine that this is working now.

    I created a test structure like this:

    [upl-image-preview url=https://www.webdeveloper.com/forum/assets/files/2018-06-26/1530017972-536145-dirstruct.png]

    and the code with GLOB_BRACE works fine.
    $dirs = glob('{blog1/posts/*,blog2/posts/*}', GLOB_BRACE);
    // new array with date as key
    $dirinfo_arr = [];
    foreach ($dirs as $cdir) {
    // read date from file
    $dirinfo_str = file_get_contents("$cdir/includes/post-info.json");
    $dirinfo = json_decode($dirinfo_str, true);
    // add current directory to the info array
    $dirinfo['dir'] = $cdir;
    // add current dir to new array where date is the key
    $dirinfo_arr[$dirinfo['Arraysortdate']] = $dirinfo;
    }
    // now we sort the new array
    krsort($dirinfo_arr);
    var_dump($dirinfo_arr);

    foreach ($dirinfo_arr as $key =&gt; $dir) { // this line changed, getting the current dir from the new array
    ?&gt;
    &lt;!--HTML--&gt;
    directory is: &lt;?php echo $dir['dir']; ?&gt;
    &lt;?php
    }
    ?&gt;
    Still don't understand what the issue is?
    Copy linkTweet thisAlerts:
    @jasewolfauthorJun 26.2018 — Ok yea I see, where I was getting an issue from was by me not having any blog post folder in the posts folder of blog2. So BRACE was causing there to be an array result to the root of blog2, as well as one to blog2/posts. But by having a blog post folder, it does it correctly.

    When I filter the array by month/categories etc, how do I go about making the array see the ``includes/title.php`</C> of the current page, to decide which filter should be applied (Same exact name in the title.php as the value in the json).

    As I will just be having one version of the array including to get what array filter should be applied, and including what the page name of the current page is (in the title.php) to decide on what value to use for the filter.

    Oh and btw I've added under the last glob for <C>
    `dir`</C> line, <C>`dirpath = $dir['dir'];``
    . Keeps it simple then in the html, especially with using includes.
    Copy linkTweet thisAlerts:
    @SempervivumJun 26.2018 — Again I have some trouble understanding where to get the filter item from. You wrote
    how do I go about making the array see the includes/title.php of the current page[/quote]
    Where is this PHP file located exactly? What does it's content look like?
    Copy linkTweet thisAlerts:
    @jasewolfauthorJun 26.2018 — @Sempervivum#1593343

    Concentrating on archives (months) right now:

    My archive folders are set out as:
    ``<i>
    </i>- archives/
    ---- 2018/
    -------- 06/
    ----------- index.php
    --------------- includes/
    --------------- title.php<i>
    </i>
    `</CODE>
    The title.php has the month name written same way as in the post-info.json files. So for 2018/06, it's <C>
    `June 2018`</C>.

    So by setting the filter to grab the title.php of the current pages via <C>
    `includes/title.php`</C>, for 2018/06, it'll be reading <C>`June 2018`` to filter to only posts of that month as stated in the post-info.json. As this is in a different directory the month pages to the posts, I assume I use the server document root option in the glob for the posts.

    By doing this using the title.php to choose the value to filter by, this will mean it'll be the same for categories/tags too.
    Copy linkTweet thisAlerts:
    @SempervivumJun 26.2018 — *more confused* What's the relation between the first directory structure I showed in post 24 and the archives structure?
    Copy linkTweet thisAlerts:
    @jasewolfauthorJun 26.2018 — Ok so with you stating jasewolf as the root, here's the blog posts along with the archives structure.
    ``<i>
    </i>jasewolf/
    blog1/posts/*blogpost*/includes/
    post-info.json
    blog2/posts/*blogpost*/includes/
    post-info.json<i>
    </i>
    `</CODE>
    <CODE>
    `<i>
    </i>jasewolf/
    archives/2018/06/includes/
    title.php<i>
    </i>
    `</CODE>
    The archives folder is placed outside of blog1 &amp; 2 because it's for both.

    For each month page, where I have the array filtering by month (same applies on a categories/tags page), I only need to link to <C>
    `includes/title.php`</C> because I'm in the month folder already.

    For the glob getting the blog posts, the difference here is I will be doing the path from the root as I am not on the blogpost pages. Here I understand I use the server document root to go start from /jasewolf.

    Getting the <C>
    `post-info.json`</C> file, the only difference there is I include <C>`$cdir`</C> in the path instead of <C>`includes`</C>.

    If I all I really am asking for regarding now how is how I use the title.php with contents
    ``June 2018" to filter the array to only blog posts with "June 2018" in the post-info.json.

    Hopefully I have this more clear!
    Copy linkTweet thisAlerts:
    @SempervivumJun 26.2018 — Sorry, still don't understand. Is this correct: You need a second, independent script for the archives directories and intend to modify the current script for use in that structure?
    Copy linkTweet thisAlerts:
    @jasewolfauthorJun 26.2018 — @Sempervivum#1593347 It's the same exact script. The only bit I'm changing is the glob path and using a $cdir on the get_contents of the post-info.json.

    The only thing I need is, filter the array using the contents of includes/title.php to posts with to same month value in the json.

    So if what I would be doing anyway to filter an array to only posts of "June 2018", is stating "June 2018" sort of like ```array_filter = ["June 2018"], I can just assign a variable to the title.php & put that variable as the value.
    Copy linkTweet thisAlerts:
    @jasewolfauthorJun 26.2018 — Though before I make any more confusion when I say same exact script, it's the same, but copied as another script php file in my blog's php scripts folder for slight different purpose of it.

    As obviously it all being in the same file, that'd obviously be conflicting.
    Copy linkTweet thisAlerts:
    @SempervivumJun 26.2018 — Hope this meets your requirement:
    // initial array containing the dirs
    $dirs = glob('archives/*/*', GLOB_ONLYDIR);
    // new array containing month
    $dirinfo_arr = [];
    foreach ($dirs as $cdir) {
    // read date from file
    $cmonth = file_get_contents("$cdir/includes/title.php");
    $dirinfo_arr[$cmonth] = $cdir;
    }
    var_dump($dirinfo_arr);

    I used this structure:

    [upl-image-preview url=https://www.webdeveloper.com/forum/assets/files/2018-06-26/1530033956-76319-jasewolf.png]

    In order to avoid variables interfering it might be recommendable to place the code inside of two functions.
    Copy linkTweet thisAlerts:
    @jasewolfauthorJun 27.2018 — Right, been doing some playing about using ``echo();`</C> to test things are turning out right. I've discovered that whilst <C>`$dirs`</C> is changing the path for each array item as it should, <C>`$cdir`</C> is just using the first array item's path. Here's an example of that happening.

    The directory I currently am in is <C>
    `{public_html}/testblog/archives/2017/04`</C>. <C>`testblog`</C> is where <C>`jasewolf`</C> is in your test structure shown in post 24. <C>`staystrong`</C> &amp; <C>`tech`</C> is the <C>`blog1`</C> &amp; <C>`blog2`</C> directories.

    The first thing in my PHP I am doing is assigning a <C>
    `$BASE_PATH`</C> to the <C>`public_html`</C> directory. Then I am globing to <C>`$dirs`</C> to each directory inside of tech/ &amp; staystrong/ <C>`posts`</C> directory using:
    <CODE>
    `<i>
    </i>// initial array containing the dirs
    $BASE_PATH = /'path/to/public_html';
    $dirs = glob($BASE_PATH.'/testblog/*/posts/*', GLOB_ONLYDIR);<i>
    </i>
    `</CODE>
    I've learnt I don't need to use GLOB_BRACE at all because I can simply put <C>
    `*/posts/*`</C> instead of specifying the 2 blog folders. The plus side with this is I can make even more blog folders if I want and the code will immediately get them too.

    Next I'm assigning <C>
    `$dirs`</C> to <C>`$cdir`</C>. and opening a new array.
    <CODE>
    `<i>
    </i>// new array with date as key
    $dirinfo_arr = [];
    foreach ($dirs as $cdir) {<i>
    </i>
    `</CODE>
    In that new array, I am grabbing the current page title "April 2017" from the current page's <C>
    `includes/title.php`</C> and assigning it to <C>`$pagetitle`</C>:
    <CODE>
    `<i>
    </i>// get current page title from file
    $pagetitle = file_get_contents("includes/title.php");<i>
    </i>
    `</CODE>
    Next I am getting the <C>
    `post-info.json`</C> for each post:
    <CODE>
    `<i>
    </i>// get date &amp; post info from file
    $dirinfo_str = file_get_contents("$cdir/includes/post-info.json");
    $dirinfo = json_decode($dirinfo_str, TRUE);<i>
    </i>
    `</CODE>
    Then I am adding the current dir to the array with date as key, then closing the array, then sorting the array:
    <CODE>
    `<i>
    </i>// add current directory to the info array
    $dirinfo['dir'] = $cdir;
    // add current dir to new array where date is the key
    $dirinfo_arr[$dirinfo['Arraysortdate']] = $dirinfo;
    }
    // now we sort the new array
    krsort($dirinfo_arr); <i>
    </i>
    `</CODE>
    Then I start the new array, as well as assigning <C>
    `$dir['dir']`</C> to <C>`$dirpath`</C>, and then remove the folders before the public_html in the path. (Also did it for <C>`$cdir`</C> for purposes of the screenshot)
    <CODE>
    `<i>
    </i>foreach($dirinfo_arr as $key=&gt;$dir) {
    $dirpath = $dir['dir'];
    $dirpath = str_replace('home/jasewolfco/public_html/', '', $dirpath);<i>
    </i>
    `</CODE>
    Now to test how things are in the array, I am using echo() before I close te array:
    <CODE>
    `<i>
    </i>echo("
    Blog post: $dirpath &lt;br&gt;
    Current page: $pagetitle &lt;br&gt;
    Blog post info: $dirinfo_str &lt;br&gt;
    Reason for error: $cdir &lt;br&gt;&lt;br&gt;
    ");
    };<i>
    </i>
    `</CODE>
    As you see in the screenshot below, <C>
    `$cdir`</C> is only getting one directory and reusing it, therefore the <C>`post-info.json`</C> data is repeating that one directory's <C>`post-info.json`</C> which it's doing not based on any of the differences I've added.<br/>
    <UPL-IMAGE-PREVIEW url="https://www.webdeveloper.com/forum/assets/files/2018-06-27/1530097609-192237-screenshot-2018-06-27-11-45-08.png">[upl-image-preview url=https://www.webdeveloper.com/forum/assets/files/2018-06-27/1530097609-192237-screenshot-2018-06-27-11-45-08.png]</UPL-IMAGE-PREVIEW>

    I'm guessing this is something to do with<C>
    `foreach ($dirs as $cdir)``
    that's causing this.
    Copy linkTweet thisAlerts:
    @SempervivumJun 27.2018 — Don't understand this completely, but I assume the reason for these issues is the following:

    You have two independent loops, the first for creating the dirinfo_arr and the second for echoing information in dirinfo_arr. Once the first loop has finished, the variables $cdir and $dirinfo_str have their final values. In your second loop these final values are not changing again and therefore the output of echo is always the same.

    In the second loop you are looping through $dirinfo_arr and assigning the current key and record to $key and $dir. You have to get all information you intend to output from these variables. If any information is not in there you need to add it in the first loop.
    Copy linkTweet thisAlerts:
    @jasewolfauthorJun 27.2018 — Ok I see, so by placing the echo into the first array, it does it correctly. Ok well such a simple reason that was then! Now to carry on working out how I could get a date match filter setup. Here is an echo output now with things correct to show the different years in the post-info. So as you see, on this page, April 2017, only the first array item should be there.[upl-image-preview url=https://www.webdeveloper.com/forum/assets/files/2018-06-27/1530111364-902558-screenshot-2018-06-27-15-45-41.png]

    p.s: it's not ordered here as the echo is before krsort.

    [Something like this](https://stackoverflow.com/questions/5808923/filter-values-from-an-array-similar-to-sql-like-search-using-php) I think is what I need, a preb_greb to take ``$pagetitle`` and match it to if it finds that as a value in the json, and if false, exclude the array item.

    Asked on [Stackoverflow](https://stackoverflow.com/questions/51072946/filter-an-array-to-only-elements-that-have-a-matching-value-to-name-in-data) regarding this, so if I get a solution there I will update or vice versa if manage to get a solution here.
    Copy linkTweet thisAlerts:
    @jasewolfauthorJun 28.2018 — Right this works an absolute charm. Though unless I state ``['Category']`</C>, or <C>`['Tag']`</C> after <C>`$dirinfo`</C>, I have to change my json to have categories &amp; tags seperated on new lines as Category/Tag1, 2, 3 etc, so I can use the same exact script for months too as well. The trouble here though is if I want to print all categories/tags of a post in the page, that's more work with them seperated like that.
    <CODE>
    `<i>
    </i>// before cycling through $dirinfo_arr for output
    if(!in_array($pagetitle, $dirinfo)) {
    continue;
    }<i>
    </i>
    `</CODE>

    Now I've added a <URL url="https://stackoverflow.com/questions/31920360/dynamic-pagination-in-jquery">[pagination js script (from StackOverFlow)](https://stackoverflow.com/questions/31920360/dynamic-pagination-in-jquery)</URL> that just takes all <C>
    `.blog-menu-item`</C> items from the page, dynamically creating a pagination.

    Three issues I've found though is there's no back/forward buttons, no numbers being added after the hash in the url so they don't stick when reloading. And then they just seem kinda odd the way they just instantly switch to content for each page because obviously it's the same page. But that most probably is just me finding it weird also with the same content duplicated for testing. So I just need to stay looking finding a really nice paginating script that just takes a particular class of content without needing to specifiy anything else.

    But anyway besides, the only thing I need to work out now is how I can expand in using the json for content to print out onto pages. So say the categories and tags, print them out hopefully also with some way of linking to the category/tag pages of the same names preferably without caps. And then by working that out how to print specific info, that'll also help with using a json for page titles and have all my specific download page info that I currently have set as seperate files being included, all in one place instead. Oh yea and then just work out how I create rss xmls which I know I have to do myself due to it being not a CMS. But working with folders, not something you really want automated having the RSS record what you may not want. But then having a test post folder or something will obviously be a good idea.

    But anyway, thanks for all the big help <USERMENTION id="158406" username="Sempervivum">@Sempervivum</USERMENTION>. Really learnt some things here from all this. Maybe it's not the best or normal method of doing things where I'm not using a database or a cms. But coding is all about learning things, maybe not the correct way and finding out why they it wasn't correct yourself. I have a fair amount of posts from over the years I need to copy over before I am actually using this, so I'll see how things go.

    Wish Flarum, Nodebb and Discourse had a solved feature so you could put the solution to the question at the top like a lot of other forums use. But anyway, I'll end this here considering I I'm done regarding array scripts in the way this thread was meant for.

    Edit: Well that is very sweet that php supports <C>
    `&amp;&amp;`</C> just like in the way that you use &amp;&amp; for commands in Unix/Linux. So now I have a script that works exactly how I want and it's just one script needed regardless if it's for tag, category or month pages by just repeating the <C>`!in_array()`</C> before closing the <C>`if()``
    .
    ×

    Success!

    Help @jasewolf spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 3.29,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,

    tipper: Anonymous,
    tipped: article
    amount: 10 SATS,
    )...