Jump to content


Photo

sort() sorting arrays - keeping index values


  • This topic is locked This topic is locked
18 replies to this topic

#1 Lang

Lang

    Young Padawan

  • Members
  • Pip
  • 198 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 09 September 2006 - 05:34 PM

$array = array('zebra', 'monkey');
echo $array[0]; //zebra
sort($array);
echo $array[0]; //monkey

As you can see when you sort an array the index values change - is it possible to sort an array in alphabetical order while keeping the index values the same? As in $array[0] stays = zebra.

Edited by Lang, 09 September 2006 - 05:40 PM.


#2 Demonslay

Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 973 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 09 September 2006 - 06:17 PM

Hmm, interesting question. Not as far as I can tell. I would store them as two different arrays or something, I honestly don't see how something like that would be possible.

#3 Lang

Lang

    Young Padawan

  • Members
  • Pip
  • 198 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 09 September 2006 - 07:25 PM

Though this probably may have been the only way it's defiitely... complicated. Had to write an algorithm for it :P

<?
	$arrayOne = array('orange', 'apple');
	$arrayTwo = $arrayOne;
	sort($arrayTwo);
	foreach ($arrayTwo as $i => $g){
		foreach ($arrayOne as $j => $h){
			if ($g == $h){
				echo $j.'=> '.$h;
			}
		}
	}
?>

That's what I came up with. Works too :o

#4 Demonslay

Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 973 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 09 September 2006 - 07:59 PM

Ha, nice idea! Never thought of simply throwing each key back into it. :D
Great job I must say. I wouldn't have thought of that for awhile (I quickly replied anyways). :D

Edited by Demonslay, 09 September 2006 - 08:00 PM.


#5 Lang

Lang

    Young Padawan

  • Members
  • Pip
  • 198 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 09 September 2006 - 08:28 PM

The only problem with doing it like that, is at every instance you want that array to be listed you have to use that code. You can't assign the values to another array because they'll just unsort themselves again.

Its a pain in the butt - hopefully someone can come up with an alternative.

#6 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 10 September 2006 - 02:20 AM

Yet another easily solved with a quick search through the php manual (not now, a few months ago, when i asked my self the same question :))
http://php.net/manua...ction.asort.php

As for Langs code, couldn't you store the value's like so:
<?
$arrayOne = array('orange', 'apple', 'something' => 'else');
$arrayTwo = $arrayOne;
sort($arrayTwo);
foreach ($arrayTwo as $i => $g){
	foreach ($arrayOne as $j => $h){
		if ($g == $h){
			$var[$j] = $h;
		}
	}
}

highlight_string(print_r($var,1));
?>

Edited by rc69, 10 September 2006 - 02:21 AM.


#7 Lang

Lang

    Young Padawan

  • Members
  • Pip
  • 198 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 10 September 2006 - 07:53 AM

Hey rc69! I did look through the manual! I swear! :)

Now, $var[$j] = $h; wouldn't work because if I were to do a foreach statement on $var it'd put all the values in order by index again. Or atleast that's what it did for me.

Edited by Lang, 10 September 2006 - 07:57 AM.


#8 cheerio

cheerio

    Young Padawan

  • Members
  • Pip
  • 246 posts
  • Gender:Male

Posted 10 September 2006 - 08:40 AM

Just use asort

#9 Demonslay

Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 973 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 10 September 2006 - 10:57 AM

LOL.
We are all idiots. :love:

Hey, I'm surprised rc didn't spot that!

#10 Lang

Lang

    Young Padawan

  • Members
  • Pip
  • 198 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 10 September 2006 - 11:33 AM

lol, he did!

He gave me a link to the funciton.

#11 Demonslay

Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 973 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 10 September 2006 - 12:09 PM

Oops, my bad. I swear, when I looked he didn't have that link... yet his edit says way earlier in the morning than I posted... rc you sneaky little *bleep*. :love:

#12 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 12 September 2006 - 01:13 AM

rc you sneaky little *bleep*. :)

Just wait till you really get to know me :)

#13 Jem

Jem

    Young Padawan

  • Members
  • Pip
  • 93 posts
  • Location:England
  • Interests:Photography, design &amp; developing, walking, cycling, reading.

Posted 12 September 2006 - 01:34 PM

I think what you're looking for is actually natcasesort.

#14 Lang

Lang

    Young Padawan

  • Members
  • Pip
  • 198 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 12 September 2006 - 05:22 PM

natcasesort() didn't work for some reason. asort() did the job.

Thanks anyways.

#15 Jem

Jem

    Young Padawan

  • Members
  • Pip
  • 93 posts
  • Location:England
  • Interests:Photography, design &amp; developing, walking, cycling, reading.

Posted 13 September 2006 - 10:59 AM

It does exactly what you asks - sorts the array while retaining the value for the specific array key, the problem is people don't generally use it "properly" - see this comment on php.net for more info. That said, if asort is working for you there's no point changing it now :P

#16 Lang

Lang

    Young Padawan

  • Members
  • Pip
  • 198 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 13 September 2006 - 11:31 AM

Well - I was using a foreach statement to fix the problem - however after use natcasesort() I lost all values in the array. I tried to output them in foreach and a regular for statement and nothing showed up. I went print_r() to see if there were any values - there was only one.

#17 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 13 September 2006 - 03:28 PM

Guy's, if it ain't broke, don't fix it. asort is working perfectly fine for his needs. If he later finds out that natcasesort would be a better option for him, then at least he knows it's an option.

#18 Jem

Jem

    Young Padawan

  • Members
  • Pip
  • 93 posts
  • Location:England
  • Interests:Photography, design &amp; developing, walking, cycling, reading.

Posted 14 September 2006 - 02:56 AM

Guy's, if it ain't broke, don't fix it. asort is working perfectly fine for his needs. If he later finds out that natcasesort would be a better option for him, then at least he knows it's an option.

...isn't that what I said? Yes, I do believe it was :rolleyes:

#19 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 14 September 2006 - 10:15 PM

Yes, i believe you're right... Ah well. Since this is solved i'm going to do what i should've done my last post and just lock this as there is no more real discussion that needs to be done (if there is, pm and i'll go ahead and re-open it).




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users