Topic: Traversing php arrays (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=30481" title="Pages that link to Topic: Traversing php arrays (Page 1 of 1)" rel="nofollow" >Topic: Traversing php arrays <span class="small">(Page 1 of 1)</span>\

 
Blaise
Paranoid (IV) Inmate

From: London
Insane since: Jun 2003

posted posted 08-12-2008 17:39

Hi there,

Following on from my previous post loading images from a folder I have an array of strings (file names loaded from a folder), and I want to pick out previous and next items in the array based on the current item.

What I know is the name of the current item, my array is reset by this time, and I want to set two variables to be the values of the previous and next items.

Here's what I have so far.

code:
function setNextPrevImages() {
  global $list, $image, $next_image, $prev_image;
		
  foreach($list as $current_image) {
    if($current_image == $image) {
      $key = $current_image;
    }
  }
  //$next_image = next($list[$key]);
  //$prev_image = prev($list[$key]);
}


The commented out lines, don't work, this is because I don't know anything about php except it's not like any other language I've used before.

This is similar to how I would expect to be able to use another scripting language, has anyone got any tips for how I can get my intended results?

Cheers,

twItch^
Maniac (V) Mad Scientist

From: Denver, CO, USA
Insane since: Aug 2000

posted posted 08-12-2008 19:28

Well, you're not returning anything from the function. Additionally, how are you using this function in the code itself? What do you want the function to do, specifically? Give you a $next_image & $prev_image link? Where is $list coming from? Is this inside a class that is fully instantiated? Help!

-S

hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana
Insane since: Aug 2000

posted posted 08-12-2008 19:36

Part of hte proble here is that you're treating $list as a hash and an array at the same time. the statement foreach($list as $current_image) doesn't guaruntee the order the items will be selected from list. The only way I can think of to safely do this is to something like

code:
$prev_image = null;
$next_image = null;
$jndx = -1;
for ($indx=0; $indx<$list.length; $indx++)
  {
    if  ($list[$indx] == $image)
      {
        $jndx = $indx;
       $key = $list[$indx];
        last;
      }
  }

$prev_image = $list[$jndx-1]     if  ($jndx > 0);
$next_image = $list[$jdnx+1]    if  ($jndx < ($list.length-1));




I haven't tested this, but, that's where I would start if I were doing this.

.



-- not necessarily stoned... just beautiful.

Blaise
Paranoid (IV) Inmate

From: London
Insane since: Jun 2003

posted posted 08-12-2008 21:33

Sorry for the poorly formed question, I'm not confident with php so I don't know what's required or not in a question

The code for creating my array is found in my second post in loading images from a folder.

My $list array has been sorted alphabetically using sort($list);

As it says in my code above $next_image and $prev_image are global variables that would be set in that function.

Here are some more details that may help.

code:
foreach ($list as $image) {
  echo "{$image}";
}


Will output 'example1.jpg'

So I assume that

code:
echo next($list);


Will output 'example2.jpg'

@hyperbole: your example looks much more familiar to what I was expecting, I'll give it a go and let you know.

Cheers,

Nathraiben
Neurotic (0) Inmate
Newly admitted

From:
Insane since: Aug 2008

posted posted 08-13-2008 06:25

Hi Blaise!

The only problem with your script I can detect right now is the fact that you're retrieving the current image's name instead of it's index. I'm on my way to work, so right now I don't have the time to test it, but this should do the trick (provided it's an indexed array, not an associative one):

code:
function setNextPrevImages() {
  global $list, $image, $next_image, $prev_image;
		
  foreach($list as $current_key => $current_image) {
    if($current_image == $image) {
      $key = $current_key;
    }
  }
  $next_image = $list[$key-1];
  $prev_image = $list[$key+1];
}

Veneficuz
Paranoid (IV) Inmate

From: A graveyard of dreams
Insane since: Mar 2001

posted posted 08-13-2008 10:47

An easier way to get the key of the image key is to use the array_search function. With that the function would look like:

code:
function setNextPrevImages() {
    global $list, $image, $next_image, $prev_image;
	
    $key = array_search($image, $list);

   // check that the $key is a sensible value
    if ($key === FALSE || !is_numeric($key)) return;
    
    $next_image = ($key < sizeof($list)-1) ? $list[$key+1] : null;
    $prev_image = ($key > 0) ? $list[$key - 1] : null;
}



Another point is that you should in general avoid using globals, but that isn't really relevant to the question at hand.

_________________________
- the Golden Ratio

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 08-13-2008 11:35

the real gotcha is that

code:
foreach ($list as $image) {
  echo "{$image}";
}


creates a *copy* of $list in order to traverse it (or at least did in php4)!.

Veneficuz' solution is much more sensible, (and has the loop in C which is quite a bit faster than the php variant.
Not that it matters much).

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

posted posted 08-13-2008 11:35

Ven! Long time, no see buddy. How've you been?

Blaise
Paranoid (IV) Inmate

From: London
Insane since: Jun 2003

posted posted 08-13-2008 12:45

Thanks for the help guys, I really appreciate this, I went with Veneficuz's method as it gave me easy access to modify what happens at the beginning and end of an array.

I'll be honest with you, I'm not using good OOP practices, and frankly it's all just 1990's hack code stuck at the top of my HTML, I'll go to hell for this, I know!

Thanks again!

Veneficuz
Paranoid (IV) Inmate

From: A graveyard of dreams
Insane since: Mar 2001

posted posted 08-13-2008 15:37
quote:
I'll be honest with you, I'm not using good OOP practices, and frankly it's all just 1990's hack code stuck at the top of my HTML, I'll go to hell for this, I know!


As long as you're aware that the code is 'hackish' then I won't complain. No reason to make the code more complex then necessary!

quote:
Ven! Long time, no see buddy. How've you been?


Rather than derail this thread, I'll answer in the now-revived Old Friends thread - that happy thread deserves to live a bit longer

_________________________
- the Golden Ratio



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu