Sometimes developers need to convert the PHP array into an object. Sometimes also need a multidimensional array to convert into an object.
Especially when you send data to API as JSON response that time multidimensional array needs to convert JSON object.
In this post, you will learn how to convert PHP arrays into JSON data.
Example 1:
This is a simple array in PHP. You can see the output of this example is an array.
$data = ['apple','banana','orange']; print_r($data); // OUTPUT Array ( [0] => apple [1] => banana [2] => orange )
It will convert into object using (object) before array. So in this example, you can see that simple PHP array how to convert into an object. the output of this example converted from an array to object.
$data = (object) ['apple','banana','orange']; print_r($data); stdClass Object ( [0] => apple [1] => banana [2] => orange )
Example 2:
In Laravel, you can see, As it is simple array output.
$data = ['apple','banana','orange']; // OUTPUT array:3 [▼ 0 => "apple" 1 => "banana" 2 => "orange" ]
Using object notation you can convert the array into an object. The output will be converted into object.
$data = (object) ['apple','banana','orange']; // OUTPUT {#260 ▼ +"0": "apple" +"1": "banana" +"2": "orange" }
Example 3:
Laravel object convert into array using (array). In this example you can see i have taken a object into $data variable, But output of that object covert into array using (array). So the output will object to array convert.
$data = new stdClass(); $data->name = 'Jony'; $data->country = 'US'; $data->age = 22; dd((array)$data); // OUTPUT array:3 [▼ "name" => "Jony" "country" => "US" "age" => 22 ]
Example 4:
Here is some info about the user like name occupation etc...
I am converting this array to JSON format. using json_encode() function. But the factor is the brother also is array inside an array. means that here multidimensional array is. So json_encode() only will work for the first array. But what about other inside array JSON formats. So you can (object) will convert into JSON data.
$data = [ 'name' => 'Monty', 'occupation' => 'Enginer', 'age' => 22, 'address' => 'London Street', 'country' => 'UK', 'brother' => (object) [ 'Vicky', 'Pointing', ] ]; dd(json_encode($data)); // OUTPUT "{"name":"Monty","occupation":"Enginer","age":22,"address":"London Street","country":"UK","brother":{"0":"Vicky","1":"Pointing"}}""
Example 5:
In this example, you will learn how to convert a PHP array into a JSON object. It will force it to convert into a JSON object.
Simple JSON encode now working for the multidimensional array that time if you want to convert into JSON, Need to force it.
$data = [ 'name' => 'Monty', 'occupation' => 'Enginer', 'age' => 22, 'address' => 'London Street', 'country' => 'UK', 'brother' => [ 'Vicky', 'Pointing', ] ]; dd(json_encode($data,JSON_FORCE_OBJECT)); // OUTPUT "{"name":"Monty","occupation":"Enginer","age":22,"address":"London Street","country":"UK","brother":{"0":"Vicky","1":"Pointing"}}"
If you are playing with API and AJAX then this post is very useful for converting data into JSON objects from database.
Very well know that the API always respons into json content and also API body always to json/Content. That the purpose this post very helpfull for that.
Thanks for reading our post.
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- How to Generate PDF File Using DomPDF In Laravel 9 Example
- How To File Upload AWS s3 Bucket In Laravel 9
- Laravel 10 CRUD Operation With YouTube Video Tutorial
- How To Filter In Laravel Query Example
- Laravel 10 Create Controller Example
- Laravel Most Useful Command's Lists With Parameters Example
- How to Send Mail using PHPMailer in Laravel?
- Join Query Same Table In Laravel
- How To Configuration Laravel Supervisor
- Convert base64 String To Image And Upload In Laravel