douwei1128 2012-02-07 19:36
浏览 64
已采纳

对simple_html_dom数组进行排序

I have been trying to get this to work for a while now!

What I am trying to do is sort two arrays, so they both get ordered depending on the values inside one of the arrays. I don't know how to "attach" the arrays so both get ordered.

Here is my code:

$html = file_get_html('http://www.amazon.co.uk/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=hat&x=0&y=0');
$test = strtolower("Beechfield Turn-up Beanie");

$arrayT = array();
$arrayP = array();

foreach ($html->find('div.product') as $results) {
    foreach ($results->find('a.title') as $title) {
        $titleLow = strtolower($title->plaintext);
        similar_text($test, $titleLow, $percent);
        $arrayT[] = $title->plaintext;
        $arrayP[] = round($percent);
    }

}

I am comparing how similar the titles brought back from the outside website are to the variable $test, which in this case is just an example.

Next I want my output to be sorted depending on the $percent variables. For example with no sorting the output would be:

title-1 55

title-2 90

title-3 66

However I want it to be sorted:

title-2 90

title-3 66

title-1 55

I have tried using array_multisort however it would only sort each array independently. I have had a look at usort and ksort as well but couldn't get a working answer.

Any help would be appreciated! I have never used any kind of sorting in PHP and have only started learning arrays so please go easy on me.

  • 写回答

1条回答 默认 最新

  • dongyin8009 2012-02-07 20:09
    关注

    I would suggest you to do this: Instead of storing title and percentage in two different array.

    you can have array indices as the titles.

    Like this:

    $html = file_get_html('http://www.amazon.co.uk/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=hat&x=0&y=0');
    $test = strtolower("Beechfield Turn-up Beanie");
    
    $arrayTP = array();
    
    
    foreach ($html->find('div.product') as $results) {
        foreach ($results->find('a.title') as $title) {
            $titleLow = strtolower($title->plaintext);
            similar_text($one, $titleLow, $percent);
            $arrayTP[$title->plaintext] = round($percent);
        }
    
    }
    

    You can sort it later using an array sort function based on the percentage. Use this: asort. Because:

    This function sorts an array such that array indices maintain their correlation with the array elements they are associated with. This is used mainly when sorting associative arrays where the actual element order is significant.

    For printing do this:

    foreach($arrayTP as $title => $percent ) {
    .
    .
    .
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?