求大神指导数组
程序员文章站
2022-03-10 14:01:07
...
我只想取出service_id,service_name,service_price,ipad_img,cart_id这几个key和他们对应的value,我该怎么办?
回复内容:
我只想取出service_id,service_name,service_price,ipad_img,cart_id这几个key和他们对应的value,我该怎么办?
简单粗暴点,参考如下
$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);
能不能贴下这个图片上的代码,好跑一下。。。
$new_arr = [];
array_walk_recursive($arr, function($item, $key) use (&$new_arr) {
$new_arr[$key] = $item;
});
print_r($new_arr);
请问你一下从数据库中取出来的数据就1条还是多条?
多条可以是下面:
$arr=[];
foreach($fruits as $k=>$v){
$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'];
}
print_R($arr);
一条的话可以使用楼上的
可以用left join ;
你的表结构大该是:
yld_cart:
cart_id | service_id | associator_id |
---|
yld_service:
service_id | service_name | service_price | ipag_img |
---|
sql语句可以这么写:
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
yii2可以这么写:
$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);
下一篇: 大数据还可以这么用,你知道吗?