如何将数组中的所有项目与 laravel 相乘?
How can I multiply all item in an array with laravel?
我在控制器中有数组,输出如下
{
"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
}
我想将数组的每个元素相乘,示例如下
(usia * wife_education * husband_education * number_of_children *
wife_religion * wife_now_working * husband_occupation * living_index *
media_exposure)
PHP foreach: http://php.net/manual/en/control-structures.foreach.php
$items = [];
foreach ($items as $item) {
// do calculation here
}
将您的数据放入字符串并在 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;
}
我在控制器中有数组,输出如下
{
"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
}
我想将数组的每个元素相乘,示例如下
(usia * wife_education * husband_education * number_of_children * wife_religion * wife_now_working * husband_occupation * living_index * media_exposure)
PHP foreach: http://php.net/manual/en/control-structures.foreach.php
$items = [];
foreach ($items as $item) {
// do calculation here
}
将您的数据放入字符串并在 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;
}