Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an Arrays internal pointer to a specific position? PHP/XML

Tags:

arrays

php

xml

Im trying to build a little site using XML instead of a database.

I would like to build a next and prev button which will work relative to the content I have displayed.

I found the php function next() and prev() as well as current() but I do not know how to set the pointer to a specific position to be able to navigate relative to the current page.

$list=array('page1','page2','page3') 

eg if im displaying contents of page2 how could I tell php i am at $list[1] so that next($list) shows page3?

Thanks in advance

like image 808
chris Avatar asked Apr 27 '09 23:04

chris


People also ask

What is internal pointer in PHP?

PHP Array iteration Using internal array pointersEach array instance contains an internal pointer. By manipulating this pointer, different elements of an array can be retrieved from the same call at different times.

Which function can be used to move the pointer to the previous array position in PHP?

The prev() function moves the internal pointer to, and outputs, the previous element in the array.

What does => mean in PHP array?

It means assign the key to $user and the variable to $pass. When you assign an array, you do it like this. $array = array("key" => "value"); It uses the same symbol for processing arrays in foreach statements. The '=>' links the key and the value.


2 Answers

If your array is always indexed consistently (eg. 'page1' is always at index '0'), it's fairly simple:

$List = array('page1', 'page2', 'page3', 'page4', 'page5'); $CurrentPage = 3; // 'page4'  while (key($List) !== $CurrentPage) next($List); // Advance until there's a match 

I personally don't rely on automatic indexing because there's always a chance that the automatic index might change. You should consider explicitly defining the keys:

$List = array(     '1' => 'page1',     '2' => 'page2',     '3' => 'page3', ); 

EDIT: If you want to test the values of the array (instead of the keys), use current():

while (current($List) !== $CurrentPage) next($List); 
like image 53
matpie Avatar answered Sep 22 '22 11:09

matpie


Using the functions below, you can get the next and previous values of the array. If current value is not valid or it is the last (first - for prev) value in the array, then:

  • the function getNextVal(...) returns the first element value
  • the function getPrevVal(...) returns the last element value

The functions are cyclic.

function getNextVal(&$array, $curr_val) {     $next = 0;     reset($array);      do     {         $tmp_val = current($array);         $res = next($array);     } while ( ($tmp_val != $curr_val) && $res );      if( $res )     {         $next = current($array);     }      return $next; }  function getPrevVal(&$array, $curr_val) {     end($array);     $prev = current($array);      do     {         $tmp_val = current($array);         $res = prev($array);     } while ( ($tmp_val != $curr_val) && $res );      if( $res )     {         $prev = current($array);     }      return $prev; } 
like image 20
Andrei Krivoshei Avatar answered Sep 21 '22 11:09

Andrei Krivoshei