Yii Framework Forum: sorting screws up listData format - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

sorting screws up listData format Rate Topic: -----

#1 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 751
  • Joined: 02-February 09

Posted 03 May 2010 - 11:17 AM

Hi. I am performing two queries. Both result sets I array_merge into one array and than I perform a listData to the merged array. However, I would like to sort the listData elements in an alphabetic order. I do that via sort(), however, than I get:
Invalid argument supplied for foreach()


Any advice how to sort the listData would be greatly appreciated.

cheers,
bettor
0

#2 User is offline   jayrulez 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 348
  • Joined: 29-July 09

Posted 03 May 2010 - 11:36 AM

View Postbettor, on 03 May 2010 - 11:17 AM, said:

Hi. I am performing two queries. Both result sets I array_merge into one array and than I perform a listData to the merged array. However, I would like to sort the listData elements in an alphabetic order. I do that via sort(), however, than I get:
Invalid argument supplied for foreach()


Any advice how to sort the listData would be greatly appreciated.

cheers,
bettor



use php's native sort()
php:
foreach(array('cat', 'dog', 'cow') as $animal) echo $animal."\n";

python:
[(animal, print(animal)) for animal in ['cat', 'dog', 'cow']]

ruby:
['cat', 'dog', 'cow'].each {|animal| puts animal}


You say Tomato, I say Tomato.
0

#3 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 751
  • Joined: 02-February 09

Posted 03 May 2010 - 12:29 PM

View Postjayrulez, on 03 May 2010 - 11:36 AM, said:

use php's native sort()


thanks for your time jayrulez. As I have mentioned in my post I have already tried sort() but it messes things up and I get the above mentioned error.
0

#4 User is offline   jayrulez 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 348
  • Joined: 29-July 09

Posted 03 May 2010 - 03:06 PM

Show a sample of the data you are trying to sort.
php:
foreach(array('cat', 'dog', 'cow') as $animal) echo $animal."\n";

python:
[(animal, print(animal)) for animal in ['cat', 'dog', 'cow']]

ruby:
['cat', 'dog', 'cow'].each {|animal| puts animal}


You say Tomato, I say Tomato.
0

#5 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 751
  • Joined: 02-February 09

Posted 03 May 2010 - 03:34 PM

View Postjayrulez, on 03 May 2010 - 03:06 PM, said:

Show a sample of the data you are trying to sort.

this doesn't work:
		$allnames=array_merge((array)$names_host,(array)$names_away);

            return sort($allnames);


this works tho without the sort:

		$allnames=array_merge((array)$names_host,(array)$names_away);

            return $allnames;


thanks jayrulez
0

#6 User is offline   jayrulez 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 348
  • Joined: 29-July 09

Posted 03 May 2010 - 05:39 PM

View Postbettor, on 03 May 2010 - 03:34 PM, said:

this doesn't work:
		$allnames=array_merge((array)$names_host,(array)$names_away);

            return sort($allnames);


this works tho without the sort:

		$allnames=array_merge((array)$names_host,(array)$names_away);

            return $allnames;


thanks jayrulez



Please have a look at http://php.net/manua...nction.sort.php

sort returns a boolean value so returning sort will return true or false.

the correct way

$allnames=array_merge((array)$names_host,(array)$names_away);
sort($allnames);
return $allnames;

php:
foreach(array('cat', 'dog', 'cow') as $animal) echo $animal."\n";

python:
[(animal, print(animal)) for animal in ['cat', 'dog', 'cow']]

ruby:
['cat', 'dog', 'cow'].each {|animal| puts animal}


You say Tomato, I say Tomato.
0

#7 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 751
  • Joined: 02-February 09

Posted 04 May 2010 - 02:07 AM

View Postjayrulez, on 03 May 2010 - 05:39 PM, said:

Please have a look at http://php.net/manua...nction.sort.php

sort returns a boolean value so returning sort will return true or false.

the correct way

$allnames=array_merge((array)$names_host,(array)$names_away);
sort($allnames);
return $allnames;



Ah, thanks for noticing this.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users