Looping over array

Hello, i need some help because i am struggling to accomplish what should be a simple task. i have no idea why this is becoming difficult. i’m unable to grasp the logic here.

i have a virtual path that i need to explode so i can use the values for various reasons. I have done this and it is working. However, i need the full path to be used to create a link. i also need the path to be subtracted to create other links. the links are virtual directories, thus i need paths to be like so:


edit: the links are structured as above but the $path is backward: 3,2,1

i cannot figure out how to loop over the exploded path and link an imploded path minus the current path. i am thinking that i need a counter here, yes?

so to paint the picture more clear:

$path = 'Animalia/Arthropoda/Insecta';
$newpath = explode("/", $path);
$currentpage = array_pop($newpath);
//so newpath is now Animalia, Arthropoda, which are links as ../ and ../../

foreach ($newpath as $key=>$value) {
  //now i can use these components for translation, content, images etc.

now i want to build a new form with links to previous pages
so the count needs to be backward as 3, 2, 1 because my links are off.
i need to loop the path from 2 to 3 not from 3 to 2.
here is code that works but it is backward 3-to-2 because of array pop.

$link = implode("/", $newpath);
foreach ($newpath as $key=>$value) {
  if ($key < 1) {
    ${value} = csrfToken($value);
  } else {
    ${value} = csrfToken($link);
    $newlink = explode("/", $link);
    $link = implode("/", $newlink);

so using array push to a new array based upon the $value solves my problem but is there a better or easier way to do this?

$newpath would contain the array after the explode:
$newpath[0] = Animalia
$newpath[1] = Arthropoda
$newpath[2] = Insecta

Your previous page variable could be built something like this:

$prevPath = “”;
for ($i = 0; $i < count($newpath) - 2;$i++)
$prevPath .= $newpath[i] . “/”;
$prevPath .= $newpath[count($newpath)-2];

-2 to skip the last item.

Be sure to put necessary array length checks in to avoid an error like if your array is only 2 entries or at root level.

Thank you for replying. Your code is definitely working but i don’t see how it is better than my current design. I currently rebuild the link with an array push and a for each loop. I’m quite satisfied with the way it is working now. I do appreciate the code because this is really what i was asking about in the first place. For some reason, i landed at array push and i like it better. Just sayin’, your code answers my original question. So Thank you! :slight_smile:

Hi John,
I have been staying out your project conversation, but felt compelled to chime in for a moment.

The “simple” problems you have are not simple because of the path you have steadfastly determined to be the way you are going to develop your project. You are going to continue to run into “simple” problems that are complicated or shouldn’t be problems at all.

My suggestion would be to make a 2.0 version of this project developed the way we would guide you to do it. I know you have spent a lot of time doing what your doing, but I believe once you getting going on a development path that the community at large would agree as the way to go about it you will find yourself moving steadily along. You can always go back to what your currently doing.

Hello Benanamen, i cannot possibly rewrite my site. I need to finish this soon. Everyday that i am chained to this site, i am missing biological work. I haven’t been in the field since October (my injury doesn’t help.) version 2 should be when i can pay someone or many people to help. Right now i am doing it all on my own. I’ve managed to learn enough php in six months to get this going. I’m really not a programmer. I’m more of a hacker of sorts. I know enough to get what i want but never really understand it. I’ve learned alot but i’m far from being able to call myself a programmer :slight_smile:

I have had some free time lately where I would likely write something up just to keep my skills sharp. I had asked you for the application code before and you sent me part of the code which did not allow me to run the app.I would need all the code. For a rewrite, the video you sent would have only been minimally useful. If you send me everything I need to run the app as you have it, I will see about getting you to the point you are with an updated architecture. My offer would cost you thousands of dollars if you had to pay someone to do it. I am offering to just do it. The world needs you out there doing your biological work.
:gorilla: :dog2: :horse::unicorn: :pig: :camel: :rhinoceros: :chipmunk: :bat: :bear::rooster: :eagle: :snake::shark::butterfly::snail::honeybee::spider::scorpion:

1 Like

Hello Benanamen, thank you for the offer but really my site is almost complete. I only needed help with simple and minor problems. I don’t see how my code will be helpful to you because i built my own router. I don’t think that my system can be changed to a new system with the current code. Thus, my code is not useful. You could build a blueprint where i just fill in the data. Really the taxonomy and the photos are all that is needed to use a new system. I thought that you would build a framework that allows me to fill in the data.

The router doesn’t matter. What matter is understanding the app and requirements. It’s impossible to watch a video and understand all the small nuances in how your app works behind the scenes (form validation, relationships etc). A modern rewrite would most probably not use any code from the original app but it’s quite necessary to get the work done :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service