-2

我在控制器中有这样的输出数组

{
  "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
 }

我想将数组的每个元素相乘,例如这样

(美国 * 妻子教育 * 丈夫教育 * 孩子人数 * 妻子宗教 * 妻子现在工作 * 丈夫职业 * 生活指数 * 媒体曝光)

4

2 回答 2

1

PHP foreach: http: //php.net/manual/en/control-structures.foreach.php

$items = [];

foreach ($items as $item) {
    // do calculation here
}
于 2018-07-10T07:54:51.147 回答
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);

然后循环$datafor乘法为:

$product = 1;
foreach ($data as $key => $value) {
    $product *= $value;
}
于 2018-07-10T09:23:21.537 回答