如何在流明中格式化json数组对象?

问题描述 投票:0回答:1

我在下面使用流明编写了一个api是我的代码。

public function customerslist(Request $request)
{
    $allTransactions = UserTransaction::where('marchant_id','=',$request->marchant_id)->get();

    foreach ($allTransactions as  $allTransaction) {
        $rows['response']="success";
        $rows['message']="Transaction";
        $customer_name = MarchantUser::where('id','=',$allTransaction->customer_id)->first();
        $response['customer_id'] = $allTransaction->customer_id;
        $response['customer_name'] = $customer_name->contact_name;
        $response['customer_ph'] = $customer_name->user_mobile_number;
        $response['customer_img'] = '';
        $response['trans_amount'] = $allTransaction->amount;
        $response['transaction_type'] = $allTransaction->transaction_type;
        $response['date']=date("d-M-Y:h:m:a",strtotime($allTransaction->created_at));
        $transaction['customer_data'][]=$response;
        $rows['customer_list']=$transaction;
    }
    echo json_encode($rows);
}

使用上面的代码,我得到的结果如下:

{
    "response": "success",
    "message": "Transaction",
    "customer_list": {
        "customer_data": [
            {
                "customer_id": "4",
                "customer_name": "anjan",
                "customer_ph": "8120653256",
                "customer_img": "",
                "trans_amount": "2000",
                "transaction_type": "debit",
                "date": "04-Jan-2020:09:01:am"
            },
            {
                "customer_id": "4",
                "customer_name": "anjan",
                "customer_ph": "8120653256",
                "customer_img": "",
                "trans_amount": "2000",
                "transaction_type": "credit",
                "date": "04-Jan-2020:10:01:am"
            },
            {
                "customer_id": "4",
                "customer_name": "anjan",
                "customer_ph": "8120653256",
                "customer_img": "",
                "trans_amount": "2000",
                "transaction_type": "credit",
                "date": "04-Jan-2020:10:01:am"
            },
            {
                "customer_id": "5",
                "customer_name": "users",
                "customer_ph": "8120653256",
                "customer_img": "",
                "trans_amount": "4000",
                "transaction_type": "debit",
                "date": "04-Jan-2020:09:01:am"
            },
            {
                "customer_id": "6",
                "customer_name": "Ganesh Ji",
                "customer_ph": "8120653250",
                "customer_img": "",
                "trans_amount": "2000",
                "transaction_type": "debit",
                "date": "06-Jan-2020:10:01:am"
            }
        ]
    }
}

但是我的预期输出应该像

  {
    "response": "success",
    "customer_list": [
      {
        "date": "04-Jan-2020",
        "customer_data": [
          {
            "customer_id": "4",
            "customer_name": "Rahul",
            "customer_img": "",
            "customer_ph": "763783438",
            "transaction_amount": "2000",
            "transaction_type": "debit"
          },
          {
            "customer_id": "4",
            "customer_name": "Anjan",
            "customer_img": "",
            "customer_ph": "57656765",
            "transaction_amount": "2000",
            "transaction_type": "advance"
          }
        ]
      },

      {
        "date": "06-01-2020",
        "customer_data": [
          {
            "customer_id": "6",
            "customer_name": "Ganesh Ji",
            "customer_img": "",
            "customer_ph": "763783438",
            "transaction_amount": "4000",
            "transaction_type": "debit"
          }
    ]
  }

客户列表必须根据交易日期具有不同的数组对象。我尝试使用group by但group by在流明中不起作用。如何基于各个日期创建一个json数组对象。

php laravel lumen
1个回答
0
投票
public function customerslist(Request $request) { $allTransactions = UserTransaction::where('marchant_id', '=', $request->marchant_id)->get(); foreach ($allTransactions as $allTransaction) { $customer_name = MarchantUser::where('id', '=', $allTransaction->customer_id)->first(); $response['customer_id'] = $allTransaction->customer_id; $response['customer_name'] = $customer_name->contact_name; $response['customer_ph'] = $customer_name->user_mobile_number; $response['customer_img'] = ''; $response['trans_amount'] = $allTransaction->amount; $response['transaction_type'] = $allTransaction->transaction_type; $temp['date'] = date("d-M-Y:h:m:a", strtotime($allTransaction->created_at)); $temp["customer_data"] = $response; $transaction[] = $temp; } $rows['response'] = "success"; $rows['message'] = "Transaction"; $rows['customer_list'] = $transaction; return $rows; }

btw $rows应该放置在foreach外部,因为只需要设置一次,并且如果没有使用json_encode,则使用return会自动对其进行编码

© www.soinside.com 2019 - 2024. All rights reserved.