ollie007 Posted June 8, 2009 Share Posted June 8, 2009 array(1,2,3,4); array[0]; array[1]; array[2]; array[3]; how do i grab the odd or even position of those arrays? Link to comment https://forums.phpfreaks.com/topic/161325-solved-how-to-grab-the-odd-and-even-values-of-array/ Share on other sites More sharing options...
gevans Posted June 8, 2009 Share Posted June 8, 2009 Do you just want them in 2 new arrays?? If so... <?php $numbers = array(1, 2, 3, 4, 5, 6, 7, ; foreach($numbers as $number){ if($number % 2) { $odd[] = $number; } else { $even[] = $number; } } Link to comment https://forums.phpfreaks.com/topic/161325-solved-how-to-grab-the-odd-and-even-values-of-array/#findComment-851304 Share on other sites More sharing options...
ollie007 Posted June 8, 2009 Author Share Posted June 8, 2009 cool !!! ty Link to comment https://forums.phpfreaks.com/topic/161325-solved-how-to-grab-the-odd-and-even-values-of-array/#findComment-851308 Share on other sites More sharing options...
ollie007 Posted June 8, 2009 Author Share Posted June 8, 2009 actually i need the position number rather than the value number?? array(10, 40, 99, 2, 66); i need array[1] array[3] etc or array[2] array[4] Link to comment https://forums.phpfreaks.com/topic/161325-solved-how-to-grab-the-odd-and-even-values-of-array/#findComment-851315 Share on other sites More sharing options...
gevans Posted June 8, 2009 Share Posted June 8, 2009 like this?? <?php $numbers = array(10, 21, 32, 46, 57, 68, 79, 80); foreach($numbers as $number =>){ if($number % 2) { $odd[] = $number; } else { $even[] = $number; } } Link to comment https://forums.phpfreaks.com/topic/161325-solved-how-to-grab-the-odd-and-even-values-of-array/#findComment-851317 Share on other sites More sharing options...
ollie007 Posted June 8, 2009 Author Share Posted June 8, 2009 i give it a shot and see if it blows up anything LOL So i only need the "=>" ? Link to comment https://forums.phpfreaks.com/topic/161325-solved-how-to-grab-the-odd-and-even-values-of-array/#findComment-851320 Share on other sites More sharing options...
gevans Posted June 8, 2009 Share Posted June 8, 2009 yup Link to comment https://forums.phpfreaks.com/topic/161325-solved-how-to-grab-the-odd-and-even-values-of-array/#findComment-851321 Share on other sites More sharing options...
ollie007 Posted June 8, 2009 Author Share Posted June 8, 2009 thanks a lot working nicely Link to comment https://forums.phpfreaks.com/topic/161325-solved-how-to-grab-the-odd-and-even-values-of-array/#findComment-851406 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.