复杂的PHP数组逻辑
我基本上需要找到一种方法来查看订购最多的产品,然后返回前5个“post_id”的数组 这是包含产品详细信息的不同订单的数组:复杂的PHP数组逻辑,php,arrays,algorithm,Php,Arrays,Algorithm,我基本上需要找到一种方法来查看订购最多的产品,然后返回前5个“post_id”的数组 这是包含产品详细信息的不同订单的数组: Array ( [1] => Array ( [post_id] => 1 [post_ident] => macbook_pro [post_name] => Macbook Pro [post_parent_ident] =&
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作为数组中的第一项返回?该数组是否来自SQL?否,产品详细信息已序列化,这是取消序列化后的输出。该数组是否来自SQL?否,产品详细信息已序列化,这是取消序列化后的输出。
$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