1.11M Members

sort array problem

 
0
 

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

 
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

 
0
 

i think their is no solution for it.

 
0
 

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

 
0
 

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.

 
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!

 
0
 

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

 
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?

Question Answered as of 1 Year Ago by cereal
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: