<?php
void public ArrayObject::uksort ( callable $cmp_function )
where,
$cmp_function = The user defined comparison function
?>
<?php
function recc01 ( $i, $j )
{
if ($i == $j)
{
return 0;
}
elseif($i > $j)
{
return -1;
}
else
{
return 1;
}
}
$arr01u = [1 => 2, 2 => 8, 3 => 18, 4 => 32, 5 => 7, 6 => 9];
$arrayObj = new ArrayObject($arr01u);
$arrayObj->uksort('recc01');
foreach ($arrayObj as $k1 => $v1)
{
echo '[ ' . $k1 . ' ] => ' . $v1 . '<br><br>';
}
?>
<?php
function recc02 ( $i, $j )
{
return $j;
}
$arr02u = [ 'SA' => "South America", 'EU' => "Europe", 'AS' => "Asia"] ;
$narrObj = new ArrayObject($arr02u);
$narrObj->uksort('recc02');
foreach ($narrObj as $k2 => $v2)
{
echo '[ ' . $k2 . ' ] => ' . $v2 . '<br><br>';
}
?>
<?php
function recc03 ( $i, $j )
{
return strcmp($j, $i);
}
$arr03u = [ 'RED', 'GREEN', 'BLUE'] ;
$narrObj = new ArrayObject($arr03u);
$narrObj->uksort('recc03');
foreach ($narrObj as $k3 => $v3)
{
echo '[ ' . $k3 . ' ] => ' . $v3 . '<br><br>';
}
?>
<?php
function recc04($i, $j)
{
$rnd04 = mt_rand(0,10);
if ($i + $j == $rnd04)
{
echo 'j = ' . $j . '<br>rand = ' . $rnd04 . '<br><br>';
return $j;
}
else
{
echo 'i = ' . $i . '<br>rand = ' . $rnd04 . '<br><br>';
return $i;
}
}
// $arr04s = [ "Apple", "Strawberry", "Pineapple", "Banana", "Orange" ];
$arr04s = [ 1, 2, 3, 4, 5 ];
$oarrObj = new ArrayObject($arr04s);
$oarrObj->uksort('recc04');
var_dump($oarrObj->uksort('recc04'));
echo '<br>';
if(uksort($arr04s, 'recc04') == true)
{
var_dump($oarrObj->uksort('recc04'));
echo '<br>';
}
?>
SOLVE ALL PROPOSED EXERCISES
NOT SOLVED YET
If possible in several versions of PHP
Especially the latest 7 and 8
EVERY NEW EXECUTION
A NEW RESULT CAN BE OBTAINED
DO THE TEST YOURSELF...
Study the data displayed carefully
<?php
function str_cmp1 ($i, $j)
{
$stcmp05 = strcmp($j, $i);
if ($stcmp05 < 0)
{
echo '[ ' . $stcmp05 . ' ] ' . $i . ' ( is GREATER than ) ' . $j . '<br><br>';
}
elseif ($stcmp05 > 0)
{
echo '[ ' . $stcmp05 . ' ] ' . $i . ' ( is LESS than ) ' . $j . '<br><br>';
}
else
{
echo '[ ' . $stcmp05 . ' ] ' . $i . ' ( is EQUAL to ) ' . $j . '<br><br>';
}
}
$arr05 = [ 'angusta', 'Ad augusta per angusta' ];
$narrObj = new ArrayObject($arr05);
$narrObj->uksort('str_cmp1');
foreach ($narrObj as $k5 => $v5)
{
echo '[ ' . $k5 . ' ] => ' . $v5 . '<br><br>';
}
?>
<?php
$arr05 = array("1"=>"today", "2" => "is", "3" => "Today");
$arrObject = new ArrayObject($arr05);
function recc05($i, $j) {
if ($i == $j)
{
return 0;
}
else if($i > $j)
{
return -1;
}
else
{
return 1;
}
}
$arrObject->uksort('recc05');
foreach($arrObject as $k5 => $v5)
{
echo '[ ' . $k5 . ' ] => ' . $v5 . '<br><br>';
}
?>