Jump to content

Incrementing a value in an array


Veltu

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
https://forums.phpfreaks.com/topic/290518-incrementing-a-value-in-an-array/
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)));

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

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! :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.