0

I basically need to find a way to see which products were ordered the most, then return an array of the top 5 'post_id' s.

This is the array of different orders that contain product details:

Array
(
    [1] => Array
        (
            [post_id] => 1
            [post_ident] => macbook_pro
            [post_name] => Macbook Pro
            [post_parent_ident] => rings
            [post_cat_ident] => default
            [post_module_ident] => store
            [post_price] => 999.00
            [post_currency] => EUR
            [item_link] => index.php?module=store&show=post&category=default&parent=rings&post=macbook_pro
            [qty] => 1
        )

)

Array
(
    [1] => Array
        (
            [post_id] => 1
            [post_ident] => macbook_pro
            [post_name] => Macbook Pro
            [post_parent_ident] => rings
            [post_cat_ident] => default
            [post_module_ident] => store
            [post_price] => 999.00
            [post_currency] => EUR
            [item_link] => index.php?module=store&show=post&category=default&parent=rings&post=macbook_pro
            [qty] => 1
        )

)

Array
(
    [1] => Array
        (
            [post_id] => 1
            [post_ident] => macbook_pro
            [post_name] => Macbook Pro
            [post_parent_ident] => rings
            [post_cat_ident] => default
            [post_module_ident] => store
            [post_price] => 999.00
            [post_currency] => EUR
            [item_link] => index.php?module=store&show=post&category=default&parent=rings&post=macbook_pro
            [qty] => 1
        )

)

How can I determine which products are in the array the most?

For example, the product with post_id 1 is in the array 3 times. How would I count that, then return the post_id as the first item in the array?

2
  • Is that array coming from SQL? Commented Apr 6, 2011 at 17:09
  • No, the product details are serialized and this is the output after unserializing. Commented Apr 6, 2011 at 17:11

1 Answer 1

5
$result = array();
foreach($array as $value)
{
   $postId = $value[1]['post_id'];
   if(isset($result[$postId])){
      $result[$postId]++;     // increment count of post id if already exists in result
   }
   else{
      $result[$postId] = 1;    // start count for post id
   }  
}

$keys = array_keys(asort($result));   // sort the array and find all the keys
echo $keys[count($keys)-1];           // last key will be the answer
Sign up to request clarification or add additional context in comments.

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.