简体   繁体   English

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

[英]How to get the output on this PHP script?

I'm a newbie to programming and PHP learning about two dimensional arrays. 我是编程和PHP学习二维数组的新手。

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'); 当我在函数调用之前放置一个echo像这样的echo usort($a, 'mysort1'); the page shows me a 1 (I have no idea why). 该页面显示1(我不知道为什么)。

What I really want to see is the output of the array after it's been sorted. 我真正想看到的是数组排序后的输出。

Doing var_dump($a); 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'); 


?> 

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. 接下来,usort对数组进行排序,如果成功,则返回TRUE,否则返回FALSE,这就是为什么在回显它的返回值时会看到1。

Try: 尝试:

print_r($a);

to display the sorted array. 显示排序的数组。

Put this as the last line... var_dump($a); 将其作为最后一行... var_dump($ a); it is a quick way to see what is in an array. 这是查看数组中内容的快速方法。

Add this: 添加:

print_r($a);

after your usort 您的伤害后

If you want to output the data nicely using HTML, you could use a foreach loop as follows: 如果要使用HTML很好地输出数据,可以使用如下所示的foreach循环:

echo "<ul>";
foreach ($a as $key => $value)
{
    echo "<li>key: ", $key, " value: ", $value, "</li>";
}
echo "</ul>";

Or something similar with tables if you like. 或者,如果您喜欢的话,可以使用表格。

usort actually sorts the array you pass to it. usort实际上对传递给它的数组进行排序。 In other words, it does not return a sorted array but performs the sort on the same array you pass it because it's passing by reference. 换句话说,它不返回已排序的数组,而是对您传递的数组进行排序,因为它是通过引用传递的。 If you look here: http://php.net/manual/en/function.usort.php you will see there is an ampersand infront of the parameter. 如果您在此处查看: http : //php.net/manual/en/function.usort.php,您将看到该参数前面有一个&符。 The return value of usort indicates success or failure. usort的返回值指示成功或失败。 Again, you can see this in the manual. 同样,您可以在手册中看到这一点。

You are printing the return value of the function usort , and not the contents of your array. 您正在打印函数usort的返回值,而不是数组的内容。 Instead try giving print_r a shot after calling usort. 而是尝试在调用usort之后尝试给print_r打个针。

Add after the usort line 在usort行之后添加

var_dump( $a );

Your page only prints "1" because it is saying that usort was successful in sorting the array. 您的页面仅打印“ 1”,因为它表示usort成功地对数组进行了排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM