从for循环中对值进行排序

时间:2017-04-02 08:50:58

标签: php

你好我试图从for循环中排序值(NUMERIC),我的代码:

$result_itemid_count = count($result_itemid);
for($x = 0; $x < $result_itemid_count; $x++) {
    echo $result_itemid[$x][0];
    echo "<br />";
}

输出:

  

172505254931 172505254931 172505254931 172505254931 172505254931   172505254931 172295676941 172295676941 172195402570 172195402570   172195402570 172295676941 172408848472 172195402570 172195402570   172593285746 172593285746 172593285746 172593285746 172263227044   172263227044 172195402570 172263227044 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>";
}

第二个循环:

$result_itemid_count = count($result_itemid);
for($x = 0; $x < $result_itemid_count; $x++) {
    echo $result_itemid[$x][0];
    echo "<br />";
}

1 个答案:

答案 0 :(得分: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>";
}