如何按特定键对数组进行排序

时间:2014-09-13 08:11:25

标签: php arrays sorting

我想在我的代码计算的 MYCOUNT 索引的基础上对数组行进行排序。 MYCOUNT KEYWORDS 中计算出的匹配字数。我有谷歌不同的功能,但我从来没有得到所需的结果。我是php的新手,我不知道该怎么做,我从来没有在google上找到可以帮助我的结果。

$word = "red pent";
$flag = true;
$main_flag = true;
$list = array();
$result = $this->db->get('products')->result_array();
foreach ($result as $product):
    $main_flag = true;
    $keywords = explode(';', $product['product_keyword']);
    $count = 0;
    foreach ($keywords as $keys) {
        $search = explode(' ', $word);
        $local_f = false;
        foreach ($search as $w) {
            if (strtolower(trim($w)) === strtolower(trim($keys))) {
                $local_f = true;
                $count++;
                break;
            }
        }
    }
    if ($count > 0) {
        $product['mycount'] = $count;
        array_push($list, $product);
    }
    if ($main_flag) {
        //array_push($list,$product);
    }
endforeach;

数组数据

Array
(
    [0] => Array
        (
            [product_id] => 5
            [product_sku] => AD021CAFOR
            [product_code] => X12343
            [product_name] => Ten aZ Cap
            [product_price] => 600
            [product_actual_price] => 2000
            [product_discount] => 70
            [product_gender] => MEN
            [product_group] => Accessories
            [product_category] => Cap
            [product_sub_category] => 
            [product_sport] => Training
            [product_weight] => 
            [outlet_id] => 1
            [product_desc] => Adidas Ten aZ Cap CLIMACOOL
            [product_short_desc] => Adidas Ten aZ Cap CLIMACOOL
            [product_added_on] => 2014-02-20
            [product_updated_on] => 
            [product_image] => image1.png
            [product_thumbnail] => image1.png;image2.png;image3.png;image4.png;image5.png;image6.png
            [product_list_image] => rightside-list1_03.png
            [product_keyword] => Red;shirt;adidas
            [mycount] => 1
        )

    [1] => Array
        (
            [product_id] => 6
            [product_sku] => AD021CAFRO
            [product_code] => X17618
            [product_name] => Ess 3S fit Cap
            [product_price] => 600
            [product_actual_price] => 2000
            [product_discount] => 70
            [product_gender] => MEN
            [product_group] => Accessories
            [product_category] => Cap
            [product_sub_category] => 
            [product_sport] => Training
            [product_weight] => 
            [outlet_id] => 1
            [product_desc] => The adidas Performance ESS 3S Cap has a curved peak and features neon adidas 3 stripes detailing on the front. The cap is made from a breathable, 100% cotton fabric and has an adjustable strap on the back.
            [product_short_desc] => Essentials 3 Stripes Cap
            [product_added_on] => 2014-02-20
            [product_updated_on] => 
            [product_image] => image1.png
            [product_thumbnail] => image1.png;image2.png;image3.png;image4.png;image5.png;image6.png
            [product_list_image] => rightside-list1_03.png
            [product_keyword] => red;pent;outfiters
            [mycount] => 2
        )

)

2 个答案:

答案 0 :(得分:2)

如果您想根据用户定义的标准进行排序,请使用允许用户定义的the sorting functions"排序,例如uasort

function sortby_mycount_asc($a, $b) {
    return $a["mycount"] - $b["mycount"];
}
uasort($product, "sortby_mycount_asc");

答案 1 :(得分:0)

它在我的案例中工作

function cmp($a, $b) {

        if ($a['mycount'] == $b['mycount']) {
            return 0;
        }
        return ($a < $b) ? 1 : -1;
    }

    uasort($list,"cmp");