duanpa1980 2011-04-12 03:10
浏览 55
已采纳

如何获取此PHP脚本的输出?

I'm a newbie to programming and PHP learning about two dimensional arrays.

When I run this code below, it just gives me a blank page. When I put an echo in front of the function call like this echo usort($a, 'mysort1'); the page shows me a 1 (I have no idea why).

What I really want to see is the output of the array after it's been sorted.

Doing var_dump($a); is not what I'm after. how do I show the result of the function?

How? Thanks if you can help.

<?php

$a = array (
array ('key1' => 940, 'key2' => 'blah'),
array ('key1' => 23, 'key2' => 'this'),
array ('key1' => 894, 'key2' => 'that')
);


function mysort1($x, $x) {
     return ($x['key1'] > $y['key']);

}

usort($a, 'mysort1'); 


?> 
  • 写回答

5条回答 默认 最新

  • drjmrg8766 2011-04-12 03:13
    关注

    For starters I think you have two typos in your comparison function:

                         //should be $y not $x
    function mysort1($x, $y) {
         return ($x['key1'] > $y['key1']);  // Should be $y['key1']
    }
    

    Next, usort sorts the array in place returning TRUE on success and FALSE otherwise, that is why you see a 1 when you echo it's return value.

    Try:

    print_r($a);
    

    to display the sorted array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?