PHP my_asort() 和 my_ksort()

PHP my_asort() and my_ksort()

我正在研究 php 中的排序问题。 我必须编写自己的排序函数,使用 my_asort() 和 my_ksort() 函数,它们分别与 asort 和 ksort 完全相同。 但是我无法获得正确的输出(我是 php 的新手),因此非常感谢任何有关如何纠正此问题的帮助。

这是我输入的代码:

<?php


echo "Original Array<br><br>";
$member = array("Jack" => "55kg", "Bill" => "35kg", "Aaron" => "60kg", "Daniel" => "80kg" );



foreach ($member as $user => $weight) {
echo "$user = $weight <br>";
}

echo "<br><br>";

function my_asort($member)
    {
    $keys=array_keys($member);
    sort($keys);
    foreach($keys as $key)
        {
        $val=$member[$key];
        unset($member[$key]);
        $member[$key]=$val;
        }
    }

echo "Sorted By user <br><br>";
foreach ($member as $user => $weight) {
echo "$user = $weight <br>";
}

echo "<br><br>";

function my_ksort($member)
    {
    $keys=array_keys($member);
    sort($keys);
    foreach($keys as $key)
        {
        $val=$member[$key];
        unset($member[$key]);
        $member[$key]=$val;
        }
    }

echo "Sorted By weight <br><br>";
foreach ($member as $user => $weight) {
echo "$user = $weight <br>";
}

?>

这是它显示的内容:(它没有进行任何排序,我做错了什么?)

Original Array

Jack = 55kg 
Bill = 35kg 
Aaron = 60kg 
Daniel = 80kg 


Sorted By user 

Jack = 55kg 
Bill = 35kg 
Aaron = 60kg 
Daniel = 80kg 

Sorted By weight 

Jack = 55kg 
Bill = 35kg 
Aaron = 60kg 
Daniel = 80kg

两个问题:

您没有使用您创建的函数:

my_asort($member);
my_ksort($member);

并且您必须在参数中的数组上添加引用 (&),因为您除了修改数组外没有返回任何内容:

function my_asort(&$member)
function my_ksort(&$member)

(我没有检查结果是否对应于实际的 asortksort 函数,但由于它看起来像学校作业,我相信你现在会发现你有你的函数的输出)

您需要调用您的函数(或使用没有函数声明的代码)。您只是在代码中定义了它们。

所以如果你想使用它们,你需要调用它们,你的代码将如下所示:

<?php

function my_ksort(&$member)
{
  $keys=array_keys($member);
  sort($keys);
  foreach($keys as $key)
  {
    $val=$member[$key];
    unset($member[$key]);
    $member[$key]=$val;
  }
}

function my_asort(&$member)
{
  $keys=array_keys($member);
  sort($keys);
  foreach($keys as $key)
  {
    $val=$member[$key];
    unset($member[$key]);
    $member[$key]=$val;
  }
}    

echo "Original Array<br><br>";
$member = array("Jack" => "55kg", "Bill" => "35kg", "Aaron" => "60kg", "Daniel" => "80kg" );
foreach ($member as $user => $weight) { 
  echo "$user = $weight <br>";
}

echo "<br><br>";

my_asort($member);
echo "Sorted By user <br><br>";
foreach ($member as $user => $weight) {
  echo "$user = $weight <br>";
}

echo "<br><br>";

my_ksort($member);
echo "Sorted By weight <br><br>";
foreach ($member as $user => $weight) {
  echo "$user = $weight <br>";
}

您没有在代码中的任何地方调用方法。您只是提供了方法定义但没有调用。

像这样构建你的数组:

$member[] = array('name' => 'Jake', 'weight' => '54');
$member[] = array('name' => 'Bill', 'weight' => '35');
$member[] = array('name' => 'Daniel', 'weight' => '80');

然后使用下面的函数:

$new_arr = array_sort($member, 'name', 'SORT_ASC');
$new_arr2 = array_sort($member, 'weight', 'SORT_DESC');

print_r($new_arr);
print_r($new_arr2);

function array_sort($array, $on, $order='SORT_ASC'){
    $new_array = array();
    $sortable_array = array();

    if (count($array) > 0) {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                foreach ($v as $k2 => $v2) {
                    if ($k2 == $on) {
                        $sortable_array[$k] = $v2;
                    }
                }
            } else {
                $sortable_array[$k] = $v;
            }
        }

        switch ($order) {
            case 'SORT_ASC':
                asort($sortable_array);
            break;
            case 'SORT_DESC':
                arsort($sortable_array);
            break;
        }

        foreach ($sortable_array as $k => $v) {
            $new_array[$k] = $array[$k];
        }
    }

    return $new_array;
}