我只想拿service_id,service_name,service_price,ipad_img,cart_id這幾個key和他們對應的value,我該怎麼辦?
我只想拿service_id,service_name,service_price,ipad_img,cart_id這幾個key和他們對應的value,我該怎麼辦?
簡單粗暴點,參考如下
<code>$newArray = []; foreach($array as $key=>$val){ $newArray[$key]['service_id'] = $val['id']['service_id']; $newArray[$key]['service_name'] = $val['detail'][0]['service_name']; $newArray[$key]['service_price'] = $val['detail'][0]['service_price']; $newArray[$key]['ipad_img'] = $val['detail'][0]['ipad_img']; $newArray[$key]['cart_id'] = $val['cart_id']['cart_id']; } var_dump($newArray);</code>
能不能貼下這個圖片上的程式碼,好跑一下。 。 。
<code class="php">$new_arr = []; array_walk_recursive($arr, function($item, $key) use (&$new_arr) { $new_arr[$key] = $item; }); print_r($new_arr);</code>
請問你一下從資料庫中取出的資料就1條還是多條?
多條可以是下面:
$arr=[];
foreach($fruits as $k=>$v){
<code>$arr[$k]['service_id'] = $v['id']['service_id']; $arr[$k]['service_name'] = $v['detail'][0]['service_name']; $arr[$k]['service_price'] = $v['detail'][0]['service_price']; $arr[$k]['ipad_img'] = $v['detail'][0]['ipad_img']; $arr[$k]['cart_id'] = $v['cart_id']['cart_id'];</code>
}
print_R($arr);
一條的話可以使用樓上的
可以用left join ;
你的表結構大該是:
yld_cart:
cart_id | service_id | associator_id |
---|
yld_service:
service_id | service_name | service_price | ipag_img |
---|
sql語句可以這麼寫:
<code class="mysql">SELECT cart_id , cart.`service_id` ,service.`service_name`,service.`service_price`,service.`ipad_img`FROM `yjd_cart` as cart LEFT JOIN `yjd_service` service ON cart.service_id = service.service_id WHERE cart.associator_id=1</code>
yii2可以這麼寫:
<code class="php">$query = new Query(); $associator_id = 1; $result = $query->select(['cart_id','yjd_cart.service_id','yjd_service.service_name','yjd_service.service_price','yjd_service.ipad_img']) ->from('yjd_cart') ->leftJoin('yjd_service','yjd_service.service_id = yjd_cart.service_id') ->where(['yjd_cart.associator_id'=>$associator_id]) ->all(); var_dump($result);</code>