How to access the elements of a function's return array?

后端 未结 15 2199
栀梦
栀梦 2020-12-05 03:46

I need to return multiple values from a function, therefore I have added them to an array and returned the array.



        
15条回答
  •  -上瘾入骨i
    2020-12-05 04:35

    here is the best way in a similar function

     function cart_stats($cart_id){
    
    $sql = "select sum(price) sum_bids, count(*) total_bids from carts_bids where cart_id = '$cart_id'";
    $rs = mysql_query($sql);
    $row = mysql_fetch_object($rs);
    $total_bids = $row->total_bids;
    $sum_bids = $row->sum_bids;
    $avarage = $sum_bids/$total_bids;
    
     $array["total_bids"] = "$total_bids";
     $array["avarage"] = " $avarage";
    
     return $array;
    }  
    

    and you get the array data like this

    $data = cart_stats($_GET['id']); 
    
    

提交回复
热议问题