2

I want to sort my array rows on the bases of MYCOUNT index which is calculated by my code. MYCOUNT is calculated number of matched words in KEYWORDS. I have google different functions but i never got required result. I am new in php i have no idea how to do it and i never found result on google which can help me.

$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 Data

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 Answers 2

2

When you want to sort based on user defined criteria you use the sorting functions that allow "user defined" sorting, such as uasort:

function sortby_mycount_asc($a, $b) {
    return $a["mycount"] - $b["mycount"];
}
uasort($product, "sortby_mycount_asc");
Sign up to request clarification or add additional context in comments.

Comments

0

Its working in my case

function cmp($a, $b) {

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

    uasort($list,"cmp");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.