function already available?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

function already available?

Post by s.dot »

Is there a PHP function to push a numerical keyed array up by one?

IE

Code: Select all

array(0 => 'foo', 1 => 'bar');
becomes

Code: Select all

array(1 => 'foo', 2 => 'bar');
Maintain the values in the same order, just bump each key up by a number.

?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post by MarK (CZ) »

I don't think there's a function for that, but one nice loop would do the trick, wouldn't it? :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

(#10850)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

I was thinking a loop or array_walk(), just felt kinda "dirty" =/
but thanks :-D
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Two functions will do the trick:

Code: Select all

$array = array(0 => 'foo', 1 => 'bar'); // numerical array you want to shift
array_unshift($array, null);
unset($array[0]);
var_dump($array);
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

This work?

Code: Select all

<?php
$ThisArray = array('0' => 'Zero','1' => 'One','2' => 'Two','3' => 'Three');
$NewArray = array();

reset($ThisArray);

while (list($Key, $Value) = each($ThisArray)) {
  $Key = $Key + 1;
  $NewArray[$Key] = $Value;
}

print_r($NewArray);
?>
Edit: You beat me :D
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

I think my way is better. ;-)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Yeah you beat me and it's better.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

i love when you blink and like four other people answer the question.

:)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

perfect, thanks
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply