Jump to content

Incrementing a value in an array


Veltu
Go to solution Solved by Barand,

Recommended Posts

Hi all,

 

I'm having trouble attempting to increment values in an array...

 

$current = 5;

$next = array(
                     pic1 => array('slide' => $current),
                     pic2 => array('slide' => $current),
                     pic3 => array('slide' => $current));
 
I basically want it to use the $current value and then within the array increment the value to get the next three values; 6,7,8.
 
Every attempt I keep getting 5,5,5 returned and I'm quite stuck. 
 
Would be great help if someone could point me in the right direction.  :)
Link to comment
Share on other sites

This is as dynamic as it could be but since you haven't really provided much info to understand what this is going to be used for and how.  This should work based on your example.

$next = array(
                     pic1 => array('slide' => ($current + 1)),
                     pic2 => array('slide' => ($current + 2)),
                     pic3 => array('slide' => ($current + 3)));
Link to comment
Share on other sites

  • Solution

Then increment $current.

 

And those array keys should be in quotes



    $current = 5;
    $next = array(
                         'pic1' => array('slide' => ++$current),
                         'pic2' => array('slide' => ++$current),
                         'pic3' => array('slide' => ++$current));

Link to comment
Share on other sites

Thanks guys. 

 

If you could just answer this. If I now attempt to decrement $current.

 

$prev = array(
                         'neg_pic1' => array('slide' => --$current),
                         'neg_pic2' => array('slide' => --$current),
                         'neg_pic3' => array('slide' => --$current));

 

 
It returns:
 
7,6,5 - This has thrown me off a bit.
 
How would I get it to negate off the $current value of 5 to return 4,3,2?
 
Thanks! :)
Edited by Veltu
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.