The array operation Post Is Last edited by lazygc520 from 2013-06-2518: 14: 38 $ SQL & nbsp ;=& nbsp; "select & nbsp; * & nbsp; from & nbsp; table & nbsp; where & nbsp; cloumn & nbsp ;... array Operations
This post was last edited by lazygc520 at 18:14:38 $ SQL = "select * from table where cloumn ...";
$ Result = mysql_query ($ SQL );
$ Var = array ();
While ($ row = mysql_fetch_array ($ result, MYSQL_NUM ))
{
$ Var [] = $ row;
}
Foreach ($ var as $ v)
{
Foreach ($ v as $ key => $ value)
{
Echo $ value .' ';
}
}
The result is as follows:
4890 // value1;
1635
624 // value2;
4890 // value3;
How can I change the result:
4890
2249 // 1635 + 624;
4890
This method cannot be used for calculation. The result is incorrect:
Foreach ($ var as $ v)
{
Foreach ($ v as $ key => $ value)
{
$ Total + = $ v [$ value];
Echo $ total;
}
}
The result is as follows:
4890
7139 // 4890 + 2249
12029 // 4890 + 2249 + 4890
Find a solution.
Share:
------ Solution --------------------
$ Var is not the array. Is it wrong?
------ Solution --------------------
What does this mean?
$ar = array(
array( 1635 ),
array( 624 ),
);
$res = array();
foreach($ar as $item) {
foreach($item as $v) $res[] = $v;
}
echo array_sum($res) . '//' . join('+', $res);
2259 // 1635 + 624