Home  >  Article  >  Backend Development  >  Ask God for guidance

Ask God for guidance

WBOY
WBOYOriginal
2016-08-30 09:36:46971browse

Ask God for guidancePicture description

Ask God for guidance

I received the cart_id from ios in the form of an array, and then returned the data to it after foreach traversal. How can I return the data in the format shown in the picture? Can anyone please guide me

Reply content:

Ask God for guidancePicture description

Ask God for guidance

I received the cart_id from ios in the form of an array, and then returned the data to it after foreach traversal. How can I return the data in the format shown in the picture? Can anyone please guide me

<code class="php">
//封装数据集合
$data = [];

foreach ($cart_id as $key => $val) {
    // 其他逻辑得到你想要的数据
    // 

    $service_name  = $serviceSqlResult['service_name']; 
    $service_price = $serviceSqlResult['service_price']; 
    $service_num   = $serviceSqlResult['service_num']; 
    $ipde_img      = $serviceSqlResult['ipde_img']; 
    $data[] = [
        'service_name'  => $service_name,
        'service_price' => $service_price,
        'service_num'   => $service_num,
        'ipde_img'      => $ipde_img,
    ];
}

//输出
echo json_encode($data);</code>

Traverse such a two-dimensional array, and then return json_encode in one click. You can refer to the one upstairs

Statement:
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