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.
1 episode• 3 mins•9 years ago
Overview
A useful tip for when you need to quickly cast an array to an object.