我基本上需要找到一种方法来查看最有序的产品,然后返回前5个'post_id'的数组。
这是包含产品详细信息的不同订单数组:
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 :(得分: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