Jump to content

PHP Object to Array help

Go to solution Solved by Aho,

First of all, $OBJECT is a JSON array, but it is invalid JSON. The JSON spec doesn't allow single quotes so json_decode will likely fail.

 

To begin, fix $OBJECT:

$OBJECT = '[{"type":"IRON_BOOTS","damage":153},{"type":"IRON_LEGGINGS","damage":153},{"type":"IRON_CHESTPLATE","damage":153},{"type":"IRON_HELMET","damage":153}]';

Decode it to a PHP object:

$foo = json_decode($OBJECT);
 
Access the values:
for ($i = 0; $i < count($foo); $i++){    echo $foo[$i]->type;    echo $foo[$i]->damage;}
 
Output (no line breaks):
 
IRON_BOOTS153IRON_LEGGINGS153IRON_CHESTPLATE153IRON_HELMET153
 
Unless I made a few typos, this will work fine.

To give a little background, i have a MineCraft server and am working on a way to display a persons inventory on its website.

 

I am using THIS plugin to write a players inventory to my database, and THIS page shows what has been written to the database (all personal information has been removed).

 

Everything in the content and armor cells are a single object, i need to break it apart to use the contents of each bracket individually. For example, i would like to break

THIS

$OBJECT="[{'type':'IRON_BOOTS','damage':153},{'type':'IRON_LEGGINGS','damage':153},{'type':'IRON_CHESTPLATE','damage':153},{'type':'IRON_HELMET','damage':153}]";

 

into THIS


$OBJECT[1]="{'type':'IRON_LEGGINGS','damage':153}";
$OBJECT[2]="{'type':'IRON_CHESTPLATE','damage':153}";
$OBJECT[3]="{'type':'IRON_HELMET','damage':153}]";

$OBJECT[0]="[{'type':'IRON_BOOTS','damage':153}";

 

 

Does anyone know how i could accomplish this? Or something similar enough to it for me to make use of the data?

 

 

Thanks

~Judah

Link to comment
https://linustechtips.com/topic/186183-php-object-to-array-help/
Share on other sites

Link to post
Share on other sites

First of all, $OBJECT is a JSON array, but it is invalid JSON. The JSON spec doesn't allow single quotes so json_decode will likely fail.

 

To begin, fix $OBJECT:

$OBJECT = '[{"type":"IRON_BOOTS","damage":153},{"type":"IRON_LEGGINGS","damage":153},{"type":"IRON_CHESTPLATE","damage":153},{"type":"IRON_HELMET","damage":153}]';

Decode it to a PHP object:

$foo = json_decode($OBJECT);
 
Access the values:
for ($i = 0; $i < count($foo); $i++){    echo $foo[$i]->type;    echo $foo[$i]->damage;}
 
Output (no line breaks):
 
IRON_BOOTS153IRON_LEGGINGS153IRON_CHESTPLATE153IRON_HELMET153
 
Unless I made a few typos, this will work fine.
Link to comment
https://linustechtips.com/topic/186183-php-object-to-array-help/#findComment-2504316
Share on other sites

Link to post
Share on other sites

 

First of all, $OBJECT is a JSON array, but it is invalid JSON. The JSON spec doesn't allow single quotes so json_decode will likely fail.

 

To begin, fix $OBJECT:

$OBJECT = '[{"type":"IRON_BOOTS","damage":153},{"type":"IRON_LEGGINGS","damage":153},{"type":"IRON_CHESTPLATE","damage":153},{"type":"IRON_HELMET","damage":153}]';

Decode it to a PHP object:

$foo = json_decode($OBJECT);
 
Access the values:
for ($i = 0; $i < count($foo); $i++){    echo $foo[$i]->type;    echo $foo[$i]->damage;}
 
Output (no line breaks):
 
IRON_BOOTS153IRON_LEGGINGS153IRON_CHESTPLATE153IRON_HELMET153
 
Unless I made a few typos, this will work fine.

 

 

 

Thanks a ton, ill update that page soon with the altered code

~Judah

Link to comment
https://linustechtips.com/topic/186183-php-object-to-array-help/#findComment-2505908
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×