compact php,php内置函数使用 compact()
這篇文章介紹的內(nèi)容是關(guān)于php內(nèi)置函數(shù)使用 compact() ,有著一定的參考價值,現(xiàn)在分享給大家,有需要的朋友可以參考一下
今天在使用tpshop時發(fā)現(xiàn)一個的php內(nèi)置函數(shù),叫compact(),該函數(shù)創(chuàng)建一個由參數(shù)所帶變量組成的數(shù)組。如果參數(shù)中存在數(shù)組,該數(shù)組中變量的值也會被獲取。
這樣可以減少點代碼量。比如/**
* 獲取購物車的價格詳情
* @param $cartList|購物車列表
* @return array
*/
public function getCartPriceInfo($cartList = null) {
$total_fee = $goods_fee = $goods_num = 0; //初始化數(shù)據(jù)。商品總額/節(jié)約金額/商品總共數(shù)量
if ($cartList) {
foreach ($cartList as $cartKey => $cartItem) {
$total_fee += $cartItem['goods_fee'];
$goods_fee += $cartItem['cut_fee'];
$goods_num += $cartItem['goods_num'];
}
}
$result = array(
'total_fee' => $total_fee,
'goods_fee' => $goods_fee,
'goods_num' => $goods_num,
);
return $result;
}
改成這樣,代碼簡潔許多。
/**
* 獲取購物車的價格詳情
* @param $cartList|購物車列表
* @return array
*/
public function getCartPriceInfo($cartList = null){
$total_fee = $goods_fee = $goods_num = 0;//初始化數(shù)據(jù)。商品總額/節(jié)約金額/商品總共數(shù)量
if($cartList){
foreach ($cartList as $cartKey => $cartItem) {
$total_fee += $cartItem['goods_fee'];
$goods_fee += $cartItem['cut_fee'];
$goods_num += $cartItem['goods_num'];
}
}
return compact('total_fee', 'goods_fee', 'goods_num');
}
相關(guān)推薦:
總結(jié)
以上是生活随笔為你收集整理的compact php,php内置函数使用 compact()的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 二元置信椭圆r语言_r语言二元期权bar
- 下一篇: php memcached 队列,red