MikeDXUNL Posted June 7, 2008 Share Posted June 7, 2008 Searched google, only to find no luck; only something on PHP.net about array arguments or something and no luck on here searching for 'even number picking' If I have an array, say [1] Array ( [0] => This is Item 1 [1] => This is Item 2 lol [2] => This is Item 3 haha [3] => This will be Item 4 [4] => and finally, this is Item 5 ) Can someone point me where I can find how to get only: $array[1][0] $array[1][2] $array[1][4] and so on, out? Quote Link to comment https://forums.phpfreaks.com/topic/109166-every-other-number/ Share on other sites More sharing options...
jjmusicpro Posted June 7, 2008 Share Posted June 7, 2008 Try a loop $placeholder = 0 Then in the loop just +2 Quote Link to comment https://forums.phpfreaks.com/topic/109166-every-other-number/#findComment-559964 Share on other sites More sharing options...
.josh Posted June 7, 2008 Share Posted June 7, 2008 right. $x = 0; while ($array[1][$x]) { //code here $x = $x + 2; } or a slightly different method: foreach($array[1] as $key => $val) { if (($key % 2) == 0) { // this position is even numbered } else { // this position is odd numbered..don't need the 'else' if you don't need to do something // special if it's odd } } Quote Link to comment https://forums.phpfreaks.com/topic/109166-every-other-number/#findComment-559978 Share on other sites More sharing options...
MikeDXUNL Posted June 7, 2008 Author Share Posted June 7, 2008 alright thanks! I chose the second method. but when inserting values into my DB there is another problem <?php $k = 1; $i = 0; $max = count($achname[1]); while($i <= $max-1) { $in_achname = mysql_real_escape_string($achname[$k][$i]); $in_achdesc = mysql_real_escape_string($achdesc[$k][$i]); foreach($achimg[1] as $key => $val) { if (($key % 2) == 0) { $in_achimg = mysql_real_escape_string($achimg[1][$key]); mysql_query("INSERT INTO achievements (gameid, achname, achdesc, achimg) VALUES ('1', '$in_achname', '$in_achdesc', '$in_achimg')") or die(mysql_error()); } else { // this position is odd numbered..don't need the 'else' if you don't need to do something // special if it's odd } } $i++; } ?> Looks like this in the DB: +-------------------------------------------------------------------------------------------+ | achid | gameid | achname | achdesc | achimg | --------------------------------------------------------------------------------------------- | 1 | 1 | The Mas... | Destr... | http://tiles.xbox.com/tiles/Ic/YE/0ICLiGJhbC9GCBoD...| | 2 | 1 | The Mas... | Destr... | http://tiles.xbox.com/tiles/LR/zN/1oCLiGJhbC83CxoD.. | | 3 | 1 | The Mas... | Destr... | http://tiles.xbox.com/tiles/Rw/+P/0oCLiGJhbC8xCxoD..| the `achimg` acutally changes but the achname and achdesc change Quote Link to comment https://forums.phpfreaks.com/topic/109166-every-other-number/#findComment-559997 Share on other sites More sharing options...
.josh Posted June 7, 2008 Share Posted June 7, 2008 I don't see you doing a $i++; in your main while loop edit: whoops nevermind didn't scroll down lol hmm maybe it's because you need to move your other vars inside the foreach loop? Quote Link to comment https://forums.phpfreaks.com/topic/109166-every-other-number/#findComment-560000 Share on other sites More sharing options...
MikeDXUNL Posted June 7, 2008 Author Share Posted June 7, 2008 EDIT: you edited your post ahah just checked my code, edited it but still same output foreach($achimg[1] as $key => $val) { $in_achname = mysql_real_escape_string($achname[$k][$i]); $in_achdesc = mysql_real_escape_string($achdesc[$k][$i]); if (($key % 2) == 0) { $in_achimg = mysql_real_escape_string($achimg[1][$key]); Quote Link to comment https://forums.phpfreaks.com/topic/109166-every-other-number/#findComment-560001 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.