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

What is data abstraction in php?

0 Answers  


A company named Pensar Technologies in chennai has offered me a job as Software Trainee in chennai.But they are asking to pay demand draft of Rs.75000/- in the name of the company.They are saying the gross renumeration will pay Rs.90000/- annually.I want to know whether this is good or bad company?Help me out in this.

18 Answers  


How is traits used in php?

0 Answers  


What is the difference between explode () and split () functions in php?

0 Answers  


Do you have to initialize variables in php?

0 Answers  






How do I sort numbers in php?

0 Answers  


Explain the visibility of the property or method?

0 Answers  


How are Variables Defined in PHP?

6 Answers   T3 Softwares,


Tell me what is the use of "ksort" in php?

0 Answers  


What is the maximum size of a database in mysql?

0 Answers  


Explain do you use composer? If yes, what benefits have you found in it?

0 Answers  


What is phpsessid?

0 Answers  


Categories