我有以下数组作为$ main_array.想要总结子数组中的所有元素,如[0] => 6,[1] => 11,[2] => 15.
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 2
[1] => 4
[2] => 5
)
[2] => Array
(
[0] => 8
[1] => 4
[2] => 3
)
)
Run Code Online (Sandbox Code Playgroud)
试过以下代码.
foreach ($main_array as $key => $value)
$main_array[$key] = Array('1'=>array_sum($value));
print_r($main_array);
Run Code Online (Sandbox Code Playgroud)
但我得到的阵列结构是,
Array
(
[0] => Array
(
[1] => 6
)
[1] => Array
(
[1] …Run Code Online (Sandbox Code Playgroud) 我有以下数组$ weight,我想将这些值存储到mysql表中的weight列.
Array
(
[0] => 12
[1] => 14
[2] => 16
[3] => 9
[4] => 7
)
Run Code Online (Sandbox Code Playgroud)
数据库表结构是,
+----------------------------+--------+------+
| Indicators | Weight | Rank |
+----------------------------+--------+------+
| Elevation | 0 | 0 |
| Distance from the sea | 0 | 0 |
| Natural Coastal Protection | 0 | 0 |
| Water Bodies | 0 | 0 |
| Precipitation | 0 | 0 |
+----------------------------+--------+------+
Run Code Online (Sandbox Code Playgroud)
我期待结果为,
+----------------------------+--------+------+
| Indicators | Weight | Rank …Run Code Online (Sandbox Code Playgroud)