convert an array to json in laravel controller

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Convert an Array to JSON in Laravel Controller Body: Hello fellow developers! In this comprehensive blog post, we will explore how to convert an array with a specific format into a desired JSON format within your Laravel controller. It will involve some simple yet essential programming techniques that are crucial for manipulating data effectively. Let's first analyze the problem and identify the ideal output for the given array: Given Array:
[612552892 => ['quantity' => '1'], 625512336 => ['quantity' => '10']]
Desired JSON Output:
[{"sku_id": 612552892, "quantity": "1"}, {"sku_id": 625512336, "quantity": "10"}]
Our goal is to generate the desired JSON format with a slight adjustment: adding the 'sku_id' key. This will help us organize and structure the data properly, making it easier for our application or other systems to consume this information. Now let's break down the process: 1. Create an empty array variable (`result`) that will be used as a JSON output container. 2. Iterate through the given array using foreach loop in Laravel. Each iteration, we check if the key is numeric or not. If it's numeric, we assume it represents a SKU ID which needs to be added as 'sku_id'. 3. For each iteration, append the modified data (with sku_id) into the output array 'result'. 4. Finally, return the result after the loop is complete and our JSON format is ready. Here's the final code: ```php public function postDetail(Request $request) { $product_requests = $request->sku; $arr = []; // Create an empty array for the JSON output container $result = []; // Iterate through product_requests, appending sku_id and quantity into result foreach ($product_requests as $key => $value) { if (is_numeric($key)) { $sku_id = $key; $quantity = $value['quantity']; } else { // Key is not numeric, so it's probably already the 'sku_id'. $sku_id = $key; $quantity = isset($value['quantity']) ? $value['quantity'] : null; } // Add the modified data (with sku_id) into result array $result[] = [ 'sku_id' => $sku_id, 'quantity' => $quantity ]; } return $result; } ``` Now that we have this code, your Laravel controller will be able to convert the given array into a desired JSON format with sku_id and quantity fields. This approach can be used in various scenarios where you need to transform data from different structures into a well-organized JSON object. Make sure to test and refine the code as per your specific requirements, and always remember the importance of proper data structuring for efficient software development. In conclusion, by following this process, we have successfully converted an array with the required format into the desired JSON output in Laravel. Properly handling data is vital for smooth application functionality, and maintaining consistent structures ensures better compatibility and future scalability.