我正在处理php中的排序问题。我必须编写自己的排序函数,分别使用与asort和ksort完全相同的my_asort()和my_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
您需要调用函数(或者使用没有函数声明的代码)。您只是在代码中定义了它们。
因此,如果你想使用它们,你需要调用它们,你的代码将看起来像:
<?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>";
}
两个问题:
您没有使用您创建的功能:
my_asort($member);
my_ksort($member);
您必须在参数中的数组上添加一个引用(&
),因为除了修改数组之外,您不会返回任何内容:
function my_asort(&$member)
function my_ksort(&$member)
(我没有检查结果是否与实际的asort
和ksort
函数相对应,但由于它看起来像是学校作业,我相信你现在会发现你有了函数的输出)
您没有在代码中的任何位置调用方法。您只提供了方法定义,但没有调用。
像这样构建数组:
$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;
}