处理数组中的数据

时间:2016-07-08 04:45:05

标签: php mysql arrays json

我正在开发一款混合移动应用。在客户端我使用html,css ajax / jquery。在服务器端(第三方)我正在使用PHP MySql。现在,当用户(app)向服务器发送ajax请求时,服务器将以json格式发回详细信息。但是在发送json数据之前,我想首先操作数组中的数据或者在数组中添加数据。

这是服务器发送的示例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"
}]

这是我的PHP代码。

$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);

如何从数据库中操作数组。

  1. 格式化和条件:我想更改所有"profit":null。如果利润是无效的'然后回声' n / a'否则回显金额(使用number_format)。以及"date_open":"2015-01-04"在Y-M-dd中格式化它。

  2. COMPUTATION :将另一个字段添加到具有"id":"11"的第一个数组的数组示例中。我想添加另一个字段,例如总计,即total =投资+利润(如果它不为空)。

  3. 我想在php服务器上这样做,因为我比在客户端更容易格式化和操作数据。因此,当我在服务器端完成此操作时,json数据已经设置并准备好显示。 I prefer to use php and not in mysql query。 谢谢。

    更新#1 :我现在可以在foreach循环中操作/更改数组。我现在的问题是如何在数组中添加数据。示例是user's investment的总和。我在循环total_investment=0之前添加了此代码,然后在循环total_investment += user_product['investment'];

    中添加了此代码

    应该显示如下:

    [{
    "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?
    }]
    

2 个答案:

答案 0 :(得分:0)

您可以在Mysql查询中执行此修改。所以,试试这个......

$userid = $_POST['user'];
$sql = "SELECT id,user_id,product,quantity,DATE_FORMAT(date_open, '%y-%M-%d') as date_open:2015-03-01,paid,harvested,reinvest,
COALESCE(profit,'N/A') as profit,investment,COALESCE(profit,0)+COALESCE(investment,0) as total
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);

如果您想在php代码中进行此操作,请按照以下代码进行操作..

$userid = $_POST["user"];
$user_products = db::select("SELECT * FROM user_products WHERE uproducts_user_id = '{$userid}'");
$product = array();
if(sizeof($user_products)>0){
foreach( $user_products as $user_product){
$profit=$user_product->profit!==null ? $user_product->profit : 0;
$arr=array(
    'id':$user_product->id,
    'user_id':$user_product->user_id,
    'product':$user_product->product,
    'quantity':$user_product->quantity,
    'date_open':$user_product->date_open,
    'paid':$user_product->paid,
    'harvested':$user_product->harvested,
    'reinvest':$user_product->reinvest,
    'profit':$profit!=0 ? $profit : 'N/A',
    'investment':$user_product->investment,
    'total':$user_product->investment+$profit
);
array_push($product, $arr);
}
}
$server_msg = $product;
echo json_encode($server_msg);

答案 1 :(得分:0)

在SQL中,我会做类似的事情:

SELECT 
id,user_id,product,quantity,
DATE_FORMAT(date_open,'%Y-%m-%d'),
paid,harvested,reinvest,
if (profit IS NULL,'n/a',FORMAT(profit,'de_DE')) as profit,
investment,
if (profit IS NOT NULL, investment+profit, NULL ) as total
FROM user_products 
WHERE uproducts_user_id = '{$userid}'