Jump to content

Help with an array algorithm


victorianrider

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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