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

How can you propagate a session id?

0 Answers  


Is laravel frontend or backend?

0 Answers  


Single choice objective PHP question...

1 Answers  


What are getters and setters php?

0 Answers  


Which one is best framework for php?

0 Answers  






Whether session will work if we disable cookies in client browser ?

10 Answers  


What is the use of print_r function in php?

0 Answers  


Which is a perfect example of runtime polymorphism?

0 Answers  


What is the difference between mysql_fetch_array() and mysql_fetch_assoc()?

0 Answers  


What is orm in php framework?

0 Answers  


Tell me what does pear stands for?

0 Answers  


Explain Traits in PHP?

0 Answers  


Categories