victorianrider Posted February 18, 2011 Share Posted February 18, 2011 Alright, so I have 100 user ids which I need sorted in a very specific way. Right now, they're organized in a 1-100 format. Let's think of each ID as ID#1, ID#2, ID#3 etc all the way up to ID#100. I need to code a script to echo them out in the following order: $ID2.'<br/>'; $ID1.'<br/>'; $ID4.'<br/>'; $ID3.'<br/>'; $ID6.'<br/>'; $ID5.'<br/>'; //etc up to $ID100.'<br/>'; $ID99.'<br/>'; Is there any special algorithm or trick with an array to do this? I will attach a file with each ID seperated by a semi-colon. And also here is the script I have right now which echo's them out in order: <?PHP $File = "IDs.txt"; $Login = file($File); $String = file_get_contents($File); $Logins = explode(";", $String); foreach($Logins as $Login) { echo $Login.'<br/>'; } ?> [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/228099-help-with-an-array-algorithm/ Share on other sites More sharing options...
BlueSkyIS Posted February 18, 2011 Share Posted February 18, 2011 if i understand, you want each pair to be in reverse order: 21436587 if so, i would use a for loop, incrementing by i + 2, then take array[i + 1] followed by array on each loop. Link to comment https://forums.phpfreaks.com/topic/228099-help-with-an-array-algorithm/#findComment-1176267 Share on other sites More sharing options...
ronverdonk Posted February 18, 2011 Share Posted February 18, 2011 Something line: $a=array('id1','id2','id3','id4','id5','id6','id7','id8','id9','id10','id11','id12'); for ($i=0, $j=1; $i<count($a); $i+=2,$j+=2) { echo "<br>$a[$j]"; echo "<br>$a[$i]"; } Link to comment https://forums.phpfreaks.com/topic/228099-help-with-an-array-algorithm/#findComment-1176276 Share on other sites More sharing options...
victorianrider Posted February 18, 2011 Author Share Posted February 18, 2011 if i understand, you want each pair to be in reverse order: 21436587 if so, i would use a for loop, incrementing by i + 2, then take array[i + 1] followed by array on each loop. That sounds about right, however I'm having a bit of trouble interpreting your suggestion into an actual script, could you please provide an example? That would be great. Cheers Link to comment https://forums.phpfreaks.com/topic/228099-help-with-an-array-algorithm/#findComment-1176282 Share on other sites More sharing options...
BlueSkyIS Posted February 18, 2011 Share Posted February 18, 2011 Something line: $a=array('id1','id2','id3','id4','id5','id6','id7','id8','id9','id10','id11','id12'); for ($i=0, $j=1; $i<count($a); $i+=2,$j+=2) { echo "<br>$a[$j]"; echo "<br>$a[$i]"; } i was thinking more like: $a=array('id1','id2','id3','id4','id5','id6','id7','id8','id9','id10','id11','id12'); $a_cnt = count($a); $sorted_vals = array() for ($i=0;$i<$a_cnt;$i++) { $sorted_vals[] = $a[$i + 1]; $sorted_vals[] = $a[$i]; } ...but if you have an odd number of values in the array, you'll need to account for that. Link to comment https://forums.phpfreaks.com/topic/228099-help-with-an-array-algorithm/#findComment-1176287 Share on other sites More sharing options...
victorianrider Posted February 18, 2011 Author Share Posted February 18, 2011 Something line: $a=array('id1','id2','id3','id4','id5','id6','id7','id8','id9','id10','id11','id12'); for ($i=0, $j=1; $i<count($a); $i+=2,$j+=2) { echo "<br>$a[$j]"; echo "<br>$a[$i]"; } Something line: $a=array('id1','id2','id3','id4','id5','id6','id7','id8','id9','id10','id11','id12'); for ($i=0, $j=1; $i<count($a); $i+=2,$j+=2) { echo "<br>$a[$j]"; echo "<br>$a[$i]"; } i was thinking more like: $a=array('id1','id2','id3','id4','id5','id6','id7','id8','id9','id10','id11','id12'); $a_cnt = count($a); $sorted_vals = array() for ($i=0;$i<$a_cnt;$i++) { $sorted_vals[] = $a[$i + 1]; $sorted_vals[] = $a[$i]; } ...but if you have an odd number of values in the array, you'll need to account for that. Yes thank you both, I will save both versions, as of right now it is working. Much appreciated for your time and help Link to comment https://forums.phpfreaks.com/topic/228099-help-with-an-array-algorithm/#findComment-1176289 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.