We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,344 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

sort array problem

I am workin on an project.in which i need to know in what order array is sorted after sortig using function sort($array);

2
Contributors
7
Replies
3 Hours
Discussion Span
3 Months Ago
Last Updated
14
Views
Question
Answered
daniel36
Junior Poster
188 posts since Nov 2011
Reputation Points: 5
Solved Threads: 3
Skill Endorsements: 0

This function sorts an array. Elements will be arranged from lowest to highest when this function has completed.

source: http://php.net/manual/en/function.sort.php

cereal
Veteran Poster
1,146 posts since Aug 2007
Reputation Points: 344
Solved Threads: 223
Skill Endorsements: 22

i think their is no solution for it.

daniel36
Junior Poster
188 posts since Nov 2011
Reputation Points: 5
Solved Threads: 3
Skill Endorsements: 0

Hmm, maybe I'm not understanding your question, can you please post your code and spot the problem?

cereal
Veteran Poster
1,146 posts since Aug 2007
Reputation Points: 344
Solved Threads: 223
Skill Endorsements: 22

if i am having the array $ab=array('0'=>'10','1'=>'5','2'=>'8') .and i sorts it using the array by $ba=sort($ab); then i need two result 1st (5,8,10) and second (1,2,0).the second is order inwhich the values is arranged.

daniel36
Junior Poster
188 posts since Nov 2011
Reputation Points: 5
Solved Threads: 3
Skill Endorsements: 0

For that you need asort():

$ab = array('0'=>'10','1'=>'5','2'=>'8');
$ba = $ab;
asort($ba);
print_r($ba);

And you get:

Array
(
    [1] => 5
    [2] => 8
    [0] => 10
)

http://www.php.net/manual/en/function.asort.php
bye!

cereal
Veteran Poster
1,146 posts since Aug 2007
Reputation Points: 344
Solved Threads: 223
Skill Endorsements: 22

but how can i get the 1st value of the sorted array?i cant use $ba[0] for 5.

daniel36
Junior Poster
188 posts since Nov 2011
Reputation Points: 5
Solved Threads: 3
Skill Endorsements: 0

A foreach loop will work:

foreach($ab as $k => $v)
{
    echo "$k  -  $v \n";
}

If you wan to group them in different arrays then use:

$keys = array();
$values = array();
foreach($ab as $k => $v)
{
    $keys[] = $k;
    $values[] = $v;
}

print_r($keys);
print_r($values);

Or better use array_keys() and array_values():

$ab = array('0'=>'10','1'=>'5','2'=>'8');
$ba = $ab;
asort($ba);

print_r(array_keys($ba));
print_r(array_values($ba));

Ok?

cereal
Veteran Poster
1,146 posts since Aug 2007
Reputation Points: 344
Solved Threads: 223
Skill Endorsements: 22
Question Answered as of 3 Months Ago by cereal

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0806 seconds using 2.69MB