Topic awaiting preservation: Traversing php arrays |
|
---|---|
Author | Thread |
Paranoid (IV) Inmate From: London |
posted 08-12-2008 17:39
Hi there, 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]); }
|
Maniac (V) Mad Scientist From: Denver, CO, USA |
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! |
Paranoid (IV) Inmate From: Madison, Indiana |
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));
|
Paranoid (IV) Inmate From: London |
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 code: foreach ($list as $image) { echo "{$image}"; }
code: echo next($list);
|
Neurotic (0) Inmate Newly admitted From: |
posted 08-13-2008 06:25
Hi Blaise! 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]; } |
Paranoid (IV) Inmate From: A graveyard of dreams |
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; }
|
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 08-13-2008 11:35
the real gotcha is that code: foreach ($list as $image) { echo "{$image}"; }
|
Maniac (V) Mad Scientist From: :morF |
posted 08-13-2008 11:35
Ven! Long time, no see buddy. How've you been? |
Paranoid (IV) Inmate From: London |
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. |
Paranoid (IV) Inmate From: A graveyard of dreams |
posted 08-13-2008 15:37
quote:
quote:
|