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

Who is the father or inventor of php?

0 Answers  


Why do we use csrf token?

0 Answers  


How can we increase execution time of a php script?

0 Answers  


Can I write php code in html file?

0 Answers  


Which is better php praogramming or cad/cam scope wise and also salaray wise?

0 Answers  


Specify array sort functions available in php ?

3 Answers  


Explain what are the different errors in php?

0 Answers  


Which php framework is best for web development?

0 Answers  


What is the use of rand() in php?

0 Answers  


what are the errors and when we get that particular errors.and who will give that errors

3 Answers  


Why is facebook still using php?

0 Answers  


can anyone tel me about the Expression engine Note:Its a CMS in Php

2 Answers  


Categories