How to Set an Arrays Internal Pointer to a Specific Position? PHP/Xml

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

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);

PHP: how to move array pointer inside foreach loop?

You can just create an ArrayIterator­Docs which is seekable­Docs.

As it is an iterator, you can change the position while you iterate over it, some rudimentary example:

foreach ($iterator as $current) {
$iterator->next();
}

It should offer everything you need out of the box. If not, you could encapsulate your needs into an Iterator­Docs on your own as well.

How to get Next or Previous Array Value Based On Value PHP

You can use array_search to get current key. Then iterate towards previous or next step

$step_master      = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$step_now = 4;
$now_index = array_search($step_now,$step_master);

echo "Now = ".$step_now ;
echo "Previous =".(isset($step_master[$now_index - 1])?$step_master[$now_index -1 ] : "");
echo "Next =".(isset($step_master[$now_index +1 ])?$step_master[$now_index +1 ] : "");

Find next and preview element in array

this simple code will help you:

<?php

function next_elm ($array, $actualUrl) {
$i = 0;
while ( $i < count($array) && $array[$i]["url"] != $actualUrl ) $i++;

if ($i < (count($array) - 1)) {
return $array[$i+1];
} else if ($i == (count($array) - 1)) {
return $array[0]; // this is depend what you want to return if the url is the last element
} else {
return false; // there is no url match
}

}
function prev_elm ($array, $actualUrl) {
$i = 0;
while ( $i < count($array) && $array[$i]["url"] != $actualUrl ) $i++;

if ($i < (count($array)) && $i>0) {
return $array[$i-1];
} else if ($i == 0) {
return $array[count($array) - 1]; // this is depend what you want to return if the url is the first element
} else {
return false; // there is no url match
}

}

PHP get previous array element knowing current array key

One option:

To set the internal pointer to a certain position, you have to forward it (using key and next, maybe do a reset before to make sure you start from the beginning of the array):

while(key($array) !== $key) next($array);

Then you can use prev():

$prev_val = prev($array);
// and to get the key
$prev_key = key($array);

Depending on what you are going to do with the array afterwards, you might want to reset the internal pointer.

If the key does not exist in the array, you have an infinite loop, but this could be solved with:

 while(key($array) !== null && key($array) !== $key)

of course prev would not give you the right value anymore but I assume the key you are searching for will be in the array anyway.

php arrays next() and prev()

next() and prev() deal with the internal pointer of the array while your script is processing.

What you need to do is find out which index of the array you are currently viewing, (array_search()) then add 1 to get the next value, and subtract 1 to get the previous value.

// Find the index of the current item
$current_index = array_search($current_id, $array);

// Find the index of the next/prev items
$next = $current_index + 1;
$prev = $current_index - 1;

Your HTML:

<?php if ($prev > 0): ?>
<a href="<?= $array[$prev] ?>">Previous</a>
<?php endif; ?>

<?php if ($next < count($array)): ?>
<a href="<?= $array[$next] ?>">Next</a>
<?php endif; ?>

PHP: Inserting a reference into an array?

The quick-and-dirty answer, but please be aware that calling this function with a reference is deprecated and may (depending on your php configuration) generate a warning:

array_splice($arr, 1, 0, array(&$var2));

The hows-and-whys answer: What's happening is pretty subtle. When you do the splice, because you have inserted a reference into that position, $var2 is actually being reassigned. You can verify it with the following code:

<?php
$hi = "test";
$var2 = "next";
$arr = array(&$hi);
$arr[] = &$var2; // this works
printf("=== var2 before splice:\n%s\n", var_export($var2, TRUE));
array_splice($arr, 1, 0, &$var2); // this doesn't
printf("=== var2 after splice:\n%s\n", var_export($var2, TRUE));
?>

You will get the following result:

=== var2 before splice:
'next'
=== var2 after splice:
array (
0 => 'next',
)

Notice that before the splice, $var2 was a string, just as you expected it to be ('next'). After the splice, though, $var2 has been replaced with an array containing one element, the string 'next'.

I think what's causing it is what the documentation says: "If replacement is not an array, it will be typecast to one (i.e. (array) $parameter)." So what is happening is this:

  • You're passing &$var2 into array as replacement.
  • Internally, php is converting &$var2 into array(&$var2). It might actually be doing something that's equivalent to $param = array($param), which means that &$var2 would be set to array(&$var2), and since it's a reference and not a copy of $var2 like it normally would be, this is affecting the variable that would normally be out of scope of the call.
  • Now it moves this new value of $var2 to the end position and inserts a copy of $var2 in the second position.

I'm not sure exactly all of the wizardry of what's happening internally, but $var is definitely being reassigned during the splice. Notice that if you use a third variable, since it's not assigning something to something that already exists as a reference, it works as expected:

<?php
$hi = "test";
$var2 = "next";
$var3 = "last";
$arr = array(&$hi);
$arr[] = &$var2; // this works
array_splice($arr, 1, 0, &$var3);
printf("=== arr is now\n%s\n", var_export($arr, TRUE));
?>

Generates the result:

=== arr is now
array (
0 => 'test',
1 => 'last',
2 => 'next',
)


Related Topics



Leave a reply



Submit