Playing
01. Quickly converting an array to an object in PHP

Episodes

0%
Your progress
  • Total: 3m
  • Played: 0m
  • Remaining: 3m
Join or sign in to track your progress

Transcript

00:00
Sometimes in PHP, you might find yourself needing to convert an array into an object. And I'm going to show you a really easy method that
00:08
allows you to do this. The first thing I'm going to do is go ahead and create an array. And this is just going to contain an items property
00:15
with another array inside of it, just so we can work with a slightly more complex data structure than just a couple of elements in here. So I'm going to say something like milk, eggs, and bread.
00:31
So we basically now have an array inside of an array. So let's do an echo on this and wrap this in pretags. And we'll use the print r to go ahead and output this. So this ends up looking like this.
00:47
We've got an outer array, an items property, which contains an array with three elements within it. So let's convert this to an object. So I'm going to create a variable called object,
00:58
just so we can establish the difference between these two items. Now, we're going to be using JSON encode and JSON decode. Now, JSON is JavaScript Object Notation.
01:09
And PHP provides us the ability to encode and decode JSON. So for example, if we wanted to convert a string, a JSON string, into an object, we can use JSON decode to do that.
01:24
An encode will take an array or something, and it will turn it into a JSON string that can be transported. But we're not using that for this purpose. We're just going to do something a little bit different,
01:35
just to convert this. So the first thing I want to do is I want to encode this array. Let's take a look at what this outputs. Or let's do a var dump on this instead.
01:51
So we've now got a string of length 33. And this is basically given us a JSON string that we would normally go ahead and store in a database or transfer somewhere else.
02:01
Now, because we've now got a string, we're in the position to use JSON decode to format this into an object. And JSON decode is going to be a JSON string.
02:13
JSON decode, by default, will convert or decode a JSON string into an object unless you provide a second option, which is a Boolean, which will be true or false.
02:25
So for example, if we were to do JSON decode on this already encoded string, and we were to say true, we get an array. So we get an array of these items. So what we've done now is we've effectively
02:43
decoded something back from it being encoded. But if we choose false here or omit this altogether, what that's going to do is it's going to turn it into an object.
02:54
So now let's do a printr on this object. And you see we've now got an object with items and then an array within it. Now, there's nothing wrong with having an array in here,
03:07
because we can still access these items. So for example, if we wanted to say something like for each object items as item, and we wanted to echo out an item depending on a space,
03:32
we can now do this. So that's basically how we convert an array into an object using PHP. Remember, we've encoded it, and then decoded it
03:42
using the JSON decode and JSON encode functions. And then we've basically got back an object from this, which we can loop through and do whatever we want to.

Episode summary

In this episode, we take a look at a super quick and handy way to convert an array into an object in PHP. We start by creating a simple array structure that actually has an array nested inside of it, just to make sure we're covering more than just a flat list. For example, we use an array with an items key, and inside that, we have another array containing things like "milk", "eggs", and "bread".

Next, we go through what this array looks like when you print it out, so you get a feel for the data format. From there, we jump into the conversion process using PHP’s built-in json_encode and json_decode functions. First, we encode the array, turning it into a JSON string, and take a quick look at the output. Then, we decode the JSON string back, but here’s the crucial part: if we don’t pass true as the second argument to json_decode, PHP will turn that JSON into an object rather than an associative array.

We verify that the structure is now an object, and even show you how you can iterate over the object's properties to access and work with the data inside. It's a neat trick that comes in handy when you want quick object-like access to your data without having to write a bunch of boilerplate code.

So, if you ever need to quickly convert an array to an object in PHP, this episode shows you exactly how to do it with just a couple of lines!

Episode discussion

No comments, yet. Be the first!