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

Tell me how to find the position of the first occurrence of a substring in a string?

0 Answers  


How to create database connection and query in php?

0 Answers  


How will you create a bi-lingual site (multiple languages) ?

3 Answers  


How to delete an element from an array?

0 Answers  


Which operator is used to concatenate two strings in PHP?

0 Answers  






List types of array are available in php?

0 Answers  


What is trim function in php?

0 Answers  


What are new features in php 7?

0 Answers  


what is array_search() in php?

2 Answers  


How to create the PHP Script to Calculate the Age Using the Inputs Of our Birth date and the Current date?

6 Answers  


What is the difference between unlink and unset ?

3 Answers  


Is php an api?

0 Answers  


Categories