简体   繁体   English

将字符串转换为php中的数组,输出然后按升序和降序输出

[英]Convert string to array in php, output it and then output in ascending and descending order

I have so far I have managed to convert a string to an array in php and out put it with a foreach and an echo statement. 到目前为止,我已经设法在php中将字符串转换为数组,并使用foreach和echo语句将其放入。 But when I try to sort it I get an error like this: 但是,当我尝试对其进行排序时,会出现如下错误:

Warning: asort() expects parameter 1 to be array. 警告:asort()期望参数1为数组。

In the text book I'm studying it shows an example like this: 在我正在研究的教科书中,它显示了这样的示例:

sort($array[,$compare]). 

I don't quite understand that. 我不太明白。 I don't want to use the print_r function. 我不想使用print_r函数。 I just want to echo out the result So I've come here to ask for help. 我只想回显结果,所以我来这里寻求帮助。 I appreciate any advice. 我感谢任何建议。 Here is my code: 这是我的代码:

 <form action="list.php" method="post">
 <input type="text" name="names">
 <br>
 <input type="submit" value="Submit">
 <?php

 if(!isset($name)) {$name = '';}
 if(!isset($names)) {$names = '';}
 if(!isset($value)) {$value = '';}
 if(!isset($myarray)) {$myarray = '';}
 $name = filter_input(INPUT_POST, 'name');
 $names = filter_input(INPUT_POST, 'names');
 $myarray = filter_input(INPUT_POST, 'myarray');
 if($myarray === NULL){
$myarray = array();
 }
 $myarray = $names;
 $name = explode(' ', $myarray);
 foreach($name as $value){
 echo ($value)."<br>";
 }
 $myarray = $names;
 $name = explode(' ', $myarray);
 foreach($name as $value){
 echo asort($value)."<br>";
 }
 $myarray = $names;
 $name = explode(' ', $myarray);
 foreach($name as $value){
 echo arsort($value)."<br>";
 }
 ?>

You have to sort before the loop. 您必须在循环之前进行排序。 Ie

asort($name);
foreach($name as $value){
    echo $value."<br>";
}
$myarray = $names;
$name = explode(' ', $myarray);
arsort($name);
foreach($name as $value){
    echo $value."<br>";
}

First off, no need to do anything with $name here: 首先,这里不需要对$name做任何事情:

$name = filter_input(INPUT_POST, 'name');

Also no need to do anything with $myarray here: 也不需要在这里用$myarray做任何事情:

$myarray = filter_input(INPUT_POST, 'myarray');

Or here: 或在这里:

if($myarray === NULL){
    $myarray = array();
}

Since you overwrite anything that could be in it here: 由于您覆盖了此处可能包含的所有内容:

$myarray = $names;

And here you overwrite $name : 在这里,您覆盖$name

$name = explode(' ', $myarray);

No need to do this either, $myarray hasn't changed since receiving this value the last time: 也不需要这样做,自从上次收到该值以来, $myarray一直没有改变:

$myarray = $names;

Sorting should be done on the array, not the values, and sort() is propably what you want here: 应该在数组上而不是在值上进行sort()sort()可能正是您想要的:

$name = asort($myarray);
foreach ($name as $value){
    echo "$value<br>";
}

And then the reverse sort: 然后反向排序:

$myarray = $names;
$name = arsort(explode(' ', $myarray));
foreach ($name as $value){
    echo "$value<br>";
}

So, for the complete code, somewhat simplified and shortened: 因此,对于完整的代码,有些简化和缩短了:

<form action="list.php" method="post">
<input type="text" name="names">
<br>
<input type="submit" value="Submit">
<?php

// This is the only variable you are posting
// Also doing explode here
$names = explode(' ', filter_input(INPUT_POST, 'names'));

// Store a copy of the array here so that we have the original unchanged
$myarray = $names;

// Printing out the array as is
foreach ($myarray as $value)
{
    // Double quotes to parse variables
    echo "$value<br>";
}

// Sort the array
sort($myarray);

// Print it out sorted
foreach ($myarray as $value)
{
    echo "$value<br>";
}

// Reverse the array
arsort($myarray);

// Print out again
foreach ($myarray as $value)
{
    echo "$value<br>";
}
?>

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

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