How can I reverse sort an array keeping the correlation
between the index and value?

Answers were Sorted based on User's Feedback



How can I reverse sort an array keeping the correlation between the index and value? ..

Answer / amit

<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" =>
"banana", "c" => "apple");
arsort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>

OUTPUT

a = orange
d = lemon
b = banana
c = apple

Is This Answer Correct ?    6 Yes 0 No

How can I reverse sort an array keeping the correlation between the index and value? ..

Answer / jude jeevanraj.p

This is done using the arsort function:

<?php
$myworld = array
("a"=>"everything","b"=>"nothing","c"=>"is");
arsort($myworld);
print_r($myworld);
?>

Which prints this:

Array
(
[b] => nothing
[c] => is
[a] => everything
)


Is This Answer Correct ?    3 Yes 6 No

Post New Answer

More PHP Interview Questions

Tools used for drawing er diagrams?

0 Answers  


Is php an object or array?

0 Answers  


What is dao in php?

0 Answers  


Can I write php code in html file?

0 Answers  


What are static variables in php?

0 Answers  


What is the difference between file_get_contents() and file_put_contents() in php?

0 Answers  


Explain me what is the difference between $_files['userfile']['name'] and $_files['userfile']['tmp_name']?

0 Answers  


How do I run a php program in dreamweaver?

0 Answers  


Which is better php or nodejs?

0 Answers  


How we get ip address of client, previous reference page etc?

0 Answers  


Why post method is used in php?

0 Answers  


Tell me what does the php error 'parse error in php - unexpected t_variable at line x' means?

0 Answers  


Categories