我的控制器中有数组,其输出是这样的
{
"usia": 0.01761252446184,
"wife_education": 0.078277886497065,
"husband_education": 0.0058708414872798,
"number_of_children": 0.17025440313112,
"wife_religion": 0.86497064579256,
"wife_now_working": 0.078277886497065,
"husband_occupation": 0.23874755381605,
"living_index": 0.078277886497065,
"media_exposure": 0.048923679060665
}
我想将数组的每个元素相乘,例如
(美国*妻子教育*丈夫教育*子女数* 老婆宗教*老婆现在工作*老公职业*生活指数* media_exposure)
答案 0 :(得分:1)
PHP foreach:http://php.net/manual/en/control-structures.foreach.php
$items = [];
foreach ($items as $item) {
// do calculation here
}
答案 1 :(得分:1)
将数据放入字符串并以JSON解码:
$data = '{
"usia": 0.01761252446184,
"wife_education": 0.078277886497065,
"husband_education": 0.0058708414872798,
"number_of_children": 0.17025440313112,
"wife_religion": 0.86497064579256,
"wife_now_working": 0.078277886497065,
"husband_occupation": 0.23874755381605,
"living_index": 0.078277886497065,
"media_exposure": 0.048923679060665
}';
$data = json_decode($data, true);
然后遍历$data
以进行乘法运算:
$product = 1;
foreach ($data as $key => $value) {
$product *= $value;
}