Toki
Toki

Reputation: 587

PHP Get specific item from this type of array

I've tried out a bunch of the answers on here and figure this should be really simple.. trying to get into a hook for a woo pdf export plugin to integrate with ACF..

If I run this -

echo $this->order

I get this -

{"id":7,"parent_id":0,"status":"processing","currency":"GBP","vers

How can I pull out just the id aka first item?

Many thanks!

SOLUTION:

As per below essentially, the code I used that works is..

$thisOrd = $this->order;
$obj = json_decode($thisOrd);
echo $obj->id;

Thanks All.

Upvotes: 0

Views: 38

Answers (3)

Moustafa Elkady
Moustafa Elkady

Reputation: 670

convert it to array first

$arr = json_decode($this->order,true);
var_dump($arr['id'])

Upvotes: 0

Thomas Wikman
Thomas Wikman

Reputation: 705

You could try to decode the json string, into a stdObject

$obj = json_decode($this->order);
echo $obj->id; // 7

Upvotes: 1

T. AKROUT
T. AKROUT

Reputation: 1717

$order = json_decode($this->order, true);
echo $order['id']

Upvotes: 0

Related Questions