2011-04-06 10 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