我只想取出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>