2011-04-06 39 views
0

私は基本的にどの製品が一番注文されたかを調べ、トップ5 'post_id'の配列を返す方法を見つける必要があります。複雑なPHPの配列ロジック

これは、製品の詳細情報が含まれている異なる順序の配列です:

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 
     ) 

) 

どのように私は、配列の中で最もである製品を決定することができますか?

たとえば、post_id 1の製品は、配列内に3回あります。どのように私はそれをカウントし、配列の最初の項目としてpost_idを返しますか?

+0

その配列はSQLに由来していますか? – thirtydot

+0

いいえ、製品の詳細はシリアル化されており、これはシリアル化解除後の出力です。 – tmartin314

答えて

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 
関連する問題