json_encode json_decode 求代码
发布网友
发布时间:2022-04-06 06:22
我来回答
共1个回答
热心网友
时间:2022-04-06 07:52
代码:
<?php
$sid = explode("|","610|629");
$id = explode("|","3171|2486");
$num = explode("|","8|5");
$money = explode("|","1|1");
$data = array();
$count = 0;
foreach($sid as $key => $val){
$data[$val] = array(
'id'=>$id[$key],
'num'=>$num[$key],
'money'=>$money[$key]
);
$count += $num[$key] * $money[$key];
}
$data['count'] = $count;
echo json_encode($data) . "\n";
print_r($data);
输出:
{"610":{"id":"3171","num":"8","money":"1"},"629":{"id":"2486","num":"5","money":"1"},"count":13}
Array
(
[610] => Array
(
[id] => 3171
[num] => 8
[money] => 1
)
[629] => Array
(
[id] => 2486
[num] => 5
[money] => 1
)
[count] => 13
)
为何重复提问?