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