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 are the steps for the payment gateway processing?

0 Answers  


Is learning php easy?

0 Answers  


What is the method to execute a php script from the command line?

0 Answers  


Swap two variables value without using third variable in php ?

0 Answers   MCN Solutions,


Are php variables global?

0 Answers  






What types of Data Can Be Used as Array Keys?

0 Answers  


What is a static variable in php?

0 Answers  


How can we submit form without a submit button?

0 Answers  


Is nan in php?

0 Answers  


Tell me what is the difference between the functions strstr() and stristr()?

0 Answers  


How can we display information of a variable and readable by a human with php?

0 Answers  


How can we register the variables into a Session?

7 Answers   HCL, Star Computers, Trikon Networks,


Categories