Veltu Posted August 18, 2014 Share Posted August 18, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/290518-incrementing-a-value-in-an-array/ Share on other sites More sharing options...
fastsol Posted August 18, 2014 Share Posted August 18, 2014 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))); Quote Link to comment https://forums.phpfreaks.com/topic/290518-incrementing-a-value-in-an-array/#findComment-1488158 Share on other sites More sharing options...
Solution Barand Posted August 18, 2014 Solution Share Posted August 18, 2014 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)); Quote Link to comment https://forums.phpfreaks.com/topic/290518-incrementing-a-value-in-an-array/#findComment-1488161 Share on other sites More sharing options...
Veltu Posted August 18, 2014 Author Share Posted August 18, 2014 (edited) 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 August 18, 2014 by Veltu Quote Link to comment https://forums.phpfreaks.com/topic/290518-incrementing-a-value-in-an-array/#findComment-1488167 Share on other sites More sharing options...
Ch0cu3r Posted August 18, 2014 Share Posted August 18, 2014 Reset $current to 5 before setting your array Quote Link to comment https://forums.phpfreaks.com/topic/290518-incrementing-a-value-in-an-array/#findComment-1488172 Share on other sites More sharing options...
Veltu Posted August 18, 2014 Author Share Posted August 18, 2014 (edited) Thank you all, Great help for a beginner Edited August 18, 2014 by Veltu Quote Link to comment https://forums.phpfreaks.com/topic/290518-incrementing-a-value-in-an-array/#findComment-1488174 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.