计算变量在数组中出现的次数

时间:2014-12-11 01:57:28

标签: php arrays session

我正在计算会话中存储的数组出现的次数:

session_start();

//product IDNO
$product_idno = isset($_POST['product_idno']) ? $_POST['product_idno'] : ""; 
// Product Qauntity
$product_qauntity = isset($_POST['product_qauntity']) ? $_POST['product_qauntity'] : "";
// Product Supplier
$product_supplier = isset($_POST['product_supplier']) ? $_POST['product_supplier'] : ""; 


$session_array = $_SESSION['portal_quote'][$product_idno] 
   = array(
       'product_qauntity' => $product_qauntity, 
       'product_supplier' => $product_supplier
   ); 

如何计算供应商在阵列中出现的次数?

1 个答案:

答案 0 :(得分:1)

您是指在该结构中出现$_SESSION['portal_quote'][i]['product_supplier']指定文字的次数?

您可以像处理PHP中的任何其他数组一样处理存储在$_SESSION 中的数组,例如:

$supplier= "Bob"; //the supplier you are looking for
$occurences = 0;

$arr = $_SESSION['portal_quote'];
foreach($arr as $record){
    if($record['product_supplier'] == $supplier){
        $occurences++;
    }
}

echo "Supplier ".$supplier." occured ".$occurences." times.";