【问题标题】:How to format json array object in lumen?如何在流明中格式化json数组对象?
【发布时间】:2020-01-07 14:28:08
【问题描述】:

我使用 lumen 编写了一个 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 数组对象。

【问题讨论】:

  • 这是一个 X-Y 问题。您无法修复 X(流明分组依据),但您认为可以通过执行 Y 来绕过它。但是您也无法执行 Y,现在正在询问如何修复 Y。不要。改为修复 X。

标签: php laravel lumen


【解决方案1】:

$transaction['customer_data'][] 更改为$transaction[]['customer_data']。但如果你想像这样制作date,你应该先制作合并数组。以下是完整代码:

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,它会自动编码

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    相关资源
    最近更新 更多