Looping over array

#1

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);
    array_pop($newlink);
    $link = implode("/", $newlink);
  }
}
#2

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?

#3

$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.

#4

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:

#5

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.