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 a composer in PHP?

0 Answers  


Questions on OOP concepts 1. What are the access specifiers available in php ? Explain 2. What is object cloning ? 3. What are the differences between interface and abstract class ? 4. What is overloading ? 5. What is overriding ? 6. How to prevent function overriding ? 7. What is the use of "final" keyword ? 8. What is static variable ? How will access a static variable ? What is static class ?

2 Answers   IPMC, NIIT,


How to block direct directory access in PHP?

0 Answers  


What is difference between action hook and filter hook?

0 Answers  


How can image properties be retrieved in php?

0 Answers  






What are new features in php 7?

0 Answers  


What is the difference between substr() and strstr()?

0 Answers  


How can we set and destroy the cookie in php?

0 Answers  


What is the use of mysql_real_escape_string() function?

0 Answers  


What is artisan in php?

0 Answers  


armstrong number by using php while number is given by the keyboard.?

0 Answers  


Is multiple inheritance supported in php?

0 Answers  


Categories