skip to Main Content

I creates an API in which I am accepting this as JSON:

{
  "productlist": [
      {"student_id": 216, "product_id": 4, "product_qty": 2},
      {"student_id": 216, "product_id": 6, "product_qty": 2}
  ]
}

I process this on server side like this:

$data = $request->json()->all();
$productlist = $data['productlist'];
$total_amount = 0;
foreach($productlist as &$pl){
    $pl['total_amount'] = $this->get_itemdetail($pl['product_id'])[0]->item_price * $pl['product_qty'];
    $pl['product_detail'] = $this->get_itemdetail($pl['product_id']);
    $total_amount+= $this->get_itemdetail($pl['product_id'])[0]->item_price * $pl['product_qty'];
}
//dd($productlist);
$productlist['cart_total'] = $total_amount;

return  response()->json(['data'=>$productlist, 'status'=> 200, 'message'=> 'Cart Processing Successfully']);

Response From API

{
"data": {
    "0": {
        "student_id": 216,
        "product_id": 4,
        "product_qty": 2,
        "total_amount": 100,
        "product_detail": [
            {
                "id": 4,
                "item_name": "Chocolate",
                "description": "dairy milk chocolate",
                "item_price": "50",
                "total_stock": 160,
            }
        ]
    },
    "1": {
        "student_id": 216,
        "product_id": 6,
        "product_qty": 2,
        "total_amount": 46,
        "product_detail": [
            {
                "id": 6,
                "item_name": "Biscuit",
                "description": "Biscuit",
                "item_price": "23",
                "total_stock": 10,
            }
        ]
    },
    "cart_total": 146
},
"status": 200,
"message": "Cart Processing Successfully"
}

But I do not want to indexing in response. Should be like this:

Desired Response

{
"data": {
     {
        "student_id": 216,
        "product_id": 4,
        "product_qty": 2,
        "total_amount": 100,
        "product_detail": [
            {
                "id": 4,
                "item_name": "Chocolate",
                "description": "dairy milk chocolate",
                "item_price": "50",
                "total_stock": 160,
            }
        ]
    },
    {
        "student_id": 216,
        "product_id": 6,
        "product_qty": 2,
        "total_amount": 46,
        "product_detail": [
            {
                "id": 6,
                "item_name": "Biscuit",
                "description": "Biscuit",
                "item_price": "23",
                "total_stock": 10,
            }
        ]
    },
    "cart_total": 146
},
"status": 200,
"message": "Cart Processing Successfully"
}

How can I get this desired response.?

2

Answers


  1. Try this

    return  response()->json(['data'=>array_values($productlist), 'status'=> 200, 'message'=> 'Cart Processing Successfully']);
    

    Or you may have to do it just before the foreach

    $productlist = array_values($data['productlist']);
    

    Basically you have string keys in the product list array, so it’s treating it like an Object when doing the JSON encode, an easy way to remove the keys is with array_values.

    I think there is a JSON flag as well (you can use when encoding), but I would have to look that up, because I forget if that is encoding or decoding….

    The proper JSON response is this

    "data": [{...},{...}]
    

    Where data is an array and not an Object. It’s an Array of Objects, basically. Objects will have keys like {"key" : "value"} in PHP this is equivalent, to either an associative array ( an array with string keys ) or the public properties of an object. To get the Array in JSON [] without keys, you need the equivalent in PHP which is a numerically indexed array.

    Login or Signup to reply.
  2. You should build your $productlist array as an associative array, not an indexed one.
    Try this: Create an empty array to store the modified product list (before foreach loop).

    $updatedProductList = [];
    

    inside the foreach loop after the $total_amount, Remove the numerical index by assigning each product to the updatedProductList array directly.

    $updatedProductList[] = $pl;
    

    and return your response like this

    return response()->json(['data' => $updatedProductList, 'status' => 200, 'message' => 'Cart Processing Successfully']);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search