Friday, April 9, 2010

difference between ksort and asort in php

Interview Question:

What is the difference between ksort and asort in php?

Answer:

ksort - sorts an array by its key
asort - sorts an array by its value

Example.

<?php
$resource = array("d" => "answers", "a" => "php", "b" => "interview", "c" => "questions");
ksort($resource);
foreach ($resource as $key => $val) {
echo "$key = $val\n";
}
?>

The sample output is

a = php b = interview c = questions d = answers

No comments:

Post a Comment