Asking God for guidance on arrays

WBOY
Release: 2016-08-25 10:37:14
Original
1060 people have browsed it

Asking God for guidance on arraysAsking God for guidance on arrays

I just want to take out the keys service_id, service_name, service_price, ipad_img, cart_id and their corresponding values. What should I do?

Reply content:

Asking God for guidance on arraysAsking God for guidance on arrays

I just want to take out the keys service_id, service_name, service_price, ipad_img, cart_id and their corresponding values. What should I do?

To be simple and crude, please refer to the following

<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>
Copy after login

Can you post the code on this picture so that I can run it? . .

<code class="php">$new_arr = [];
array_walk_recursive($arr, function($item, $key) use (&$new_arr) {
    $new_arr[$key] = $item;
});
print_r($new_arr);</code>
Copy after login

Could you please tell me whether there is only one piece of data taken out from the database or multiple pieces?
Multiple items can be as follows:
$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>
Copy after login

}
print_R($arr);
If you want one, you can use the one upstairs

You can use left join;
Your table structure should be:
yld_cart:

cart_id service_id associator_id

yld_service:

service_id service_name service_price ipag_img

The sql statement can be written like this:

<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>
Copy after login

yii2 can be written like this:

<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>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!