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 ) {
    .
    .
    .
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路