Now my question is that I have items description and price coming from input form.
not in your example you dont.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Good day everyone. Please I have a project which am sending an api through. the data should be
$data = [
"invoice_number" => "001",
"items" => [
"item" => [
"description" => "item1",
"price" => "10"
],
"item" => [
"description" => "item2",
"price" => "20",
]
]
]
Now my question is that I have items description and price coming from input form.
<input type = "text" name="description[]">
<input type = "text" name="description[]">
How do I loop the request into the api data items above. Kindly assist thanks.
Now my question is that I have items description and price coming from input form.
not in your example you dont.
@automica Sorry what do you mean? All I want is something like
foreach($request->description as $key => $ i)
{
"item" => [
"description" => $request->description[$key],
"price" => $request->price[$key]
]
}
So the items should loop inside the items array because I don't know how many items will be coming from the form maybe one or more than one.
not sure how you are preparing your input fields but you could do the following:
<input type = "text" name="products[0][description]">
<input type = "text" name="products[0][price]">
<input type = "text" name="products[1][description]">
<input type = "text" name="products[1][price]">
and then you can do
foreach($request->products as $key => $values)
{
"item" => [
"description" => $values['description'],
"price" => $values['price'],
]
}
@automica I understand you point. I can handle this type of form request. Here is the full details of my question. I am working on ewayGst indian and I have this json to be sent through API
$arrayVar = [
"supplyType" => "O",
"subSupplyType" => "1",
"subSupplyDesc" => "",
"docType" => "INV",
"docNo" => "13",
"docDate" => "07/07/2017",
"fromGstin" => "29AEKPV7203E1Z9",
"fromTrdName" => "welton",
"fromAddr1" => "2ND CROSS NO 59 19 A",
"fromAddr2" => "GROUND FLOOR OSBORNE ROAD",
"fromPlace" => "FRAZER TOWN",
"fromPincode" => 560090,
"actFromStateCode" => 29,
"fromStateCode" => 29,
"toGstin" => "02EHFPS5910D2Z0",
"toTrdName" => "sthuthya",
"toAddr1" => "Shree Nilaya",
"toAddr2" => "Dasarahosahalli",
"toPlace" => "Beml Nagar",
"toPincode" => 560090,
"actToStateCode" => 29,
"toStateCode" => 27,
"transactionType" => 4,
"otherValue" => "-100",
"totalValue" => 56099,
"cgstValue" => 0,
"sgstValue" => 0,
"igstValue" => 300.67,
"cessValue" => 400.56,
"cessNonAdvolValue" => 400,
"totInvValue" => 68358,
"transporterId" => "",
"transporterName" => "",
"transDocNo" => "",
"transMode" => "1",
"transDistance" => "100",
"transDocDate" => "",
"vehicleNo" => "PVC1234",
"vehicleType" => "R",
"itemList" => [
[
"productName" => "Wheat",
"productDesc" => "Wheat",
"hsnCode" => 1001,
"quantity" => 4,
"qtyUnit" => "BOX",
"cgstRate" => 0,
"sgstRate" => 0,
"igstRate" => 3,
"cessRate" => 3,
"cessNonadvol" => 0,
"taxableAmount" => 5609889,
],
],
];
Now all data value are coming front input form. Of course Items will be array as shown above which you loop through and that is correct. But the problem now is the I cannot loop through this data here like this
$arrayVar = [
"supplyType" => "O",
"subSupplyType" => "1",
"subSupplyDesc" => "",
"docType" => "INV",
"docNo" => "13",
"docDate" => "07/07/2017",
"fromGstin" => "29AEKPV7203E1Z9",
"fromTrdName" => "welton",
"fromAddr1" => "2ND CROSS NO 59 19 A",
"fromAddr2" => "GROUND FLOOR OSBORNE ROAD",
"fromPlace" => "FRAZER TOWN",
"fromPincode" => 560090,
"actFromStateCode" => 29,
"fromStateCode" => 29,
"toGstin" => "02EHFPS5910D2Z0",
"toTrdName" => "sthuthya",
"toAddr1" => "Shree Nilaya",
"toAddr2" => "Dasarahosahalli",
"toPlace" => "Beml Nagar",
"toPincode" => 560090,
"actToStateCode" => 29,
"toStateCode" => 27,
"transactionType" => 4,
"otherValue" => "-100",
"totalValue" => 56099,
"cgstValue" => 0,
"sgstValue" => 0,
"igstValue" => 300.67,
"cessValue" => 400.56,
"cessNonAdvolValue" => 400,
"totInvValue" => 68358,
"transporterId" => "",
"transporterName" => "",
"transDocNo" => "",
"transMode" => "1",
"transDistance" => "100",
"transDocDate" => "",
"vehicleNo" => "PVC1234",
"vehicleType" => "R",
"itemList" => [
foreach($request->products as $key => $values)
{
[ "productName" => $request->code[$item],
"productDesc" => $request->description[$item],
"hsnCode" => $request->hsncode[$item],
"quantity" => $request->quantity[$item],
"qtyUnit" => "BOX",
"cgstRate" => 0,
"sgstRate" => 0,
"igstRate" => 3,
"cessRate" => 3,
"cessNonadvol" => 0,
"taxableAmount" => 5609889,
],
}
],
];
So I am looking for a better way to pass the array of item list to the json data instead of looping through inside the data which is not possible.
Thank you for your assistance
@delyn12 just FYI, looping has been discussed on this forum many times, I even gave an example here:
https://gist.github.com/jimgwhit/cbbe5bb0d2556fdc7e37a86d3630239c
But there are also many past post on some very complex looping through Json.
You basically figure out the level and logically work it out.
You need consistent data, meaning one time you can't have five levels and one time another number of levels your routine has to work every time with the level of nesting.
Please or to participate in this conversation.