5 Replies - 637 Views - Last Post: 13 April 2009 - 03:18 PM Rate Topic: -----

#1 sharmistha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-November 08

Sorting an array

Post icon  Posted 10 April 2009 - 06:08 AM

Hello,
I know how an array has been sorted..but I want to know these following type of sorting..
Supose I have an array...
array('national','sports','home','politics','weather');
I want to display first element of array by home and next display others element..Here you can see ' home ' value situated in 3rd position in array but if I consider an array where we do not know the home value position .How can I do this??

Is This A Good Question/Topic? 0
  • +

Replies To: Sorting an array

#2 manzoor  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 13
  • View blog
  • Posts: 468
  • Joined: 07-August 07

Re: Sorting an array

Posted 10 April 2009 - 06:36 AM

Search for 'home' using array_search() function. This function will return the key if found. So after that you can assign the first element in array to 'home'.
Was This Post Helpful? 0
  • +
  • -

#3 Anderseta  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 23
  • Joined: 12-April 09

Re: Sorting an array

Posted 12 April 2009 - 01:02 AM

Or without using the array_search()....



$varArray = array('national','sports','home','politics','weather');
$firstValue = $varArray[0]; 

for ($i=0;$i<=(sizeof($varArray)-1);$i++)
{
	if ($varArray[$i] == 'home')
   {
	 $varArray[$i] = $firstValue;
	 $varArray[0] = 'home';
   }
}




I can't test it right now, but i believe this should do the trick, if not, tell me the error.
Was This Post Helpful? 0
  • +
  • -

#4 SpaceMan  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 10
  • View blog
  • Posts: 507
  • Joined: 20-February 03

Re: Sorting an array

Posted 12 April 2009 - 09:27 AM

think i would try a sort key if possible..
like an order by
$a =array('national'=>0,'sports'=>3,'home'=>1,'politics'=>2,'weather'=>12);
usort()?

or other way,
array(0=>'national', ........


kinda depends how dynamic you need it to be.

the function you have is static, but if that does the trick, great...
Was This Post Helpful? 0
  • +
  • -

#5 Phil Miller  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-April 09

Re: Sorting an array

Posted 13 April 2009 - 09:36 AM

You can use usort for custom sorting, but the order of the whole array will probably be changed in the process.

<?php

function home_sort($a, $b) {
	if ($a == "home") return -1;
	else if ($b == "home") return 1;
	else return 0;
}

$a = array('national', 'sports', 'home', 'politics', 'weather');

print_r($a);
usort($a, 'home_sort');
print_r($a);


gives the output

Array
(
	[0] => national
	[1] => sports
	[2] => home
	[3] => politics
	[4] => weather
)
Array
(
	[0] => home
	[1] => weather
	[2] => politics
	[3] => national
	[4] => sports
)

Was This Post Helpful? 0
  • +
  • -

#6 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Sorting an array

Posted 13 April 2009 - 03:18 PM

If you want to sort the array according to the keys just use the function ksort. However, if you want to sort according to the values within the array just use natsort.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1