У меня есть массив, который я хочу добавить на него еще один массив. В настоящее время сервер (php) отвечает на этот массив как данные json. Вот образец.Добавить данные как массив
[{
"id":"11",
"user_id":"8000",
"product":"Shoes A",
"quantity":"1",
"date_open":"2015-01-04",
"paid":"1",
"harvested":"",
"reinvest":null,
"profit":null,
"investment":"3000"
},
{
"id":"12",
"user_id":"8000",
"product":"Shoes B",
"quantity":"1",
"date_open":"2015-03-01",
"paid":"1",
"harvested":"",
"reinvest":null,
"profit":200,
"investment":"1500"
}]
Тогда код этого здесь.
$userid = $_POST['user'];
$sql = "SELECT * FROM user_products WHERE uproducts_user_id = '{$userid}'";
$user_products = db::select($sql);
$product = array();
foreach($user_products as $user_product){
array_push($product, $user_product);
}
$server_msg = $product;
echo json_encode($server_msg);
Теперь я хочу добавить массив, который является суммой пользовательских запросов. Так что я имею в виду код будет выглядеть так:
$product = array();
$total_product= 0;
foreach($user_products as $user_product){
$total_product+= $user_product['investment']; // the sum of investment how to show this as an array?
array_push($product, $user_product);
}
$server_msg = $product;
Таким образом, приведенный выше код, как я могу добиться этого, так что ответ сервера будет выглядеть так:
[{
"id":"11",
"user_id":"8000",
"product":"Shoes A",
"quantity":"1",
"date_open":"2015-01-04",
"paid":"1",
"harvested":"",
"reinvest":null,
"profit":null,
"investment":"3000"
},
{
"id":"12",
"user_id":"8000",
"product":"Shoes B",
"quantity":"1",
"date_open":"2015-03-01",
"paid":"1",
"harvested":"",
"reinvest":null,
"profit":200,
"investment":"1500"
},
{
"total_investment":"4500" // how can I add this here?
}]
Спасибо за помощь! @rexmac –