Php 对for循环中的值进行排序
您好,我正在尝试对for循环中的值(数字)进行排序,我的代码:Php 对for循环中的值进行排序,php,Php,您好,我正在尝试对for循环中的值(数字)进行排序,我的代码: $result_itemid_count = count($result_itemid); for($x = 0; $x < $result_itemid_count; $x++) { echo $result_itemid[$x][0]; echo "<br />"; } $result\u itemid\u count=count($result\u itemid); 对于($x=0;$xac
$result_itemid_count = count($result_itemid);
for($x = 0; $x < $result_itemid_count; $x++) {
echo $result_itemid[$x][0];
echo "<br />";
}
$result\u itemid\u count=count($result\u itemid);
对于($x=0;$x<$result\u itemid\u count;$x++){
echo$result_itemid[$x][0];
回声“
”;
}
输出:
172505254931 172505254931 172505254931 172505254931 172505254931 172505254931
172505254931 172295676941 172295676941 172195402570 172195402570
172195402570 172295676941 172408848472 172195402570 172195402570
172593285746 172593285746 172593285746 172593285746 172263227044
17226327044 172195402570 17226327044 172408848472 172408848472
我不知道如何排序,我试过排序,usort,ksort
有什么想法吗
第一个循环:
if ($resp->ack == "Success") {
$results = '';
$result_itemid = array();
// If the response was loaded, parse it and build links
foreach($resp->searchResult->item as $item) {
$pic = $item->galleryURL;
$link = $item->viewItemURL;
$title = $item->title;
$location = $item->location;
$itemid = $item->itemId;
$multiv = $item->isMultiVariationListing;
if($multiv == "true")
{
$result_itemid[] = [$itemid, $title];
} else {
// For each SearchResultItem node, build a link and append it to $results
$results .= "<tr><td align = 'center'><img src=\"$pic\"></td><td><a href=\"$link\">$title - $location</a></td></tr>";
}
}
}
// If the response does not indicate 'Success,' print an error
else {
$results = "<h3>Oops! The request was not successful. Make sure you are using a valid ";
$results .= "AppID for the Production environment.</h3>";
}
if($resp->ack==“成功”){
$results='';
$result_itemid=array();
//如果已加载响应,则解析它并构建链接
foreach($resp->searchResult->item as$item){
$pic=$item->galleryURL;
$link=$item->viewItemURL;
$title=$item->title;
$location=$item->location;
$itemid=$item->itemid;
$multiv=$item->isMultiVariationListing;
如果($multiv==“true”)
{
$result_itemid[]=[$itemid,$title];
}否则{
//对于每个SearchResultItem节点,构建一个链接并将其附加到$results
$results.=”;
}
}
}
//如果响应未指示“成功”,则打印错误
否则{
$results=“Oops!请求未成功。请确保使用的是有效的”;
$results.=“生产环境的AppID。”;
}
第二个循环:
$result_itemid_count = count($result_itemid);
for($x = 0; $x < $result_itemid_count; $x++) {
echo $result_itemid[$x][0];
echo "<br />";
}
$result\u itemid\u count=count($result\u itemid);
对于($x=0;$x<$result\u itemid\u count;$x++){
echo$result_itemid[$x][0];
回声“
”;
}
我希望你能像下面这样
$result_itemid[0][0]=121223;
$result_itemid[1][0]=121883;
$result_itemid[2][0]=121203;
$result_itemid[3][0]=121213;
$result_itemid[4][0]=121235;
$new_array=[];
$result_itemid_count = count($result_itemid);
for($x = 0; $x < $result_itemid_count; $x++) {
echo $result_itemid[$x][0];
echo "<br />";
array_push($new_array, $result_itemid[$x][0]);
}
echo "<hr/>";
sort($new_array);
$result_count = count($new_array);
for($x = 0; $x < $result_count; $x++) {
echo $new_array[$x];
echo "<br>";
}
$result\u itemid[0][0]=121223;
$result_itemid[1][0]=121883;
$result_itemid[2][0]=121203;
$result_itemid[3][0]=121213;
$result_itemid[4][0]=121235;
$new_数组=[];
$result\u itemid\u count=计数($result\u itemid);
对于($x=0;$x<$result\u itemid\u count;$x++){
echo$result_itemid[$x][0];
回声“
”;
数组推送($new\u数组,$result\u itemid[$x][0]);
}
回声“
”;
排序($new_数组);
$result\u count=count($new\u数组);
对于($x=0;$x<$result\u count;$x++){
echo$new_数组[$x];
回声“
”;
}
祝你好运。如果您遇到任何问题,甚至在某个时候遇到问题,请告诉我们。你也应该看看:真的吗?我只是需要帮助,我张贴了一些链接,将帮助您改进您的问题。由于这是一个问答网站(问答),在我们给你答案之前,实际上需要有一个问题。在您的示例中,我看不到任何排序尝试。我们也不知道原始数组是什么样子,您试图排序什么,或者预期结果应该是什么样子。排序可以是升序/降序,因为您有一个多维数组,应该对哪些键/值进行排序等等。我的问题是如何对循环中的值进行排序。按数字排序是的,如果你像@AlonEitan建议的那样做的话<代码>$a[0]和$b[0]
而不仅仅是$a
和$b
。手册甚至给出了一个使用多维数组的示例(在示例中只比较整数而不是字符串)。