Write a PHP script to sort the following associative array
- برمجة بي اتش بي
- 2021-09-09
- mhanasmh00489829403
الأجوبة
<?php
echo "
Associative array : Ascending order sort by value
";
$array2=array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40"); asort($array2);
foreach($array2 as $y=>$y_value)
{
echo "Age of ".$y." is : ".$y_value."
";
}
echo "
Associative array : Ascending order sort by Key
";
$array3=array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40"); ksort($array3);
foreach($array3 as $y=>$y_value)
{
echo "Age of ".$y." is : ".$y_value."
";
}
echo "
Associative array : Descending order sorting by Value
";
$age=array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40");
arsort($age);
foreach($age as $y=>$y_value)
{
echo "Age of ".$y." is : ".$y_value."
";
}
echo "
Associative array : Descending order sorting by Key
";
$array4=array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40"); krsort($array4);
foreach($array4 as $y=>$y_value)
{
echo "Age of ".$y." is : ".$y_value."
";
}
?>
Sample Output:
Associative array : Ascending order sort by value
Age of Sophia is : 31
Age of William is : 39
Age of Ramesh is : 40
Age of Jacob is : 41
Associative array : Ascending order sort by Key
Age of Jacob is : 41
Age of Ramesh is : 40
Age of Sophia is : 31
Age of William is : 39
Associative array : Descending order sorting by Value
Age of Jacob is : 41
Age of Ramesh is : 40
Age of William is : 39
Age of Sophia is : 31
Associative array : Descending order sorting by Key
Age of William is : 39
Age of Sophia is : 31
Age of Ramesh is : 40
Age of Jacob is : 41أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال