0

I have an array witch looks like that:

Array ( [id] => 311 [file] => [name] => Mobilny [minutes] => [connection_type] => [price] => [price_landline] => [price_mobile] => [prices] => [{"city_id":"304","months":"0","price":"1","minutes":"0"}] [link] => mobilny [page_id] => 3521 [hidden_number] => Y [position] => 0 [date] => 2016-07-26 [date_modify] => 2016-08-29 )

And this array has this column:

[prices] => [{"city_id":"304","months":"0","price":"1","minutes":"0"}]

My question is, is this value is treated like a string?

[{"city_id":"304","months":"0","price":"1","minutes":"0"}]

How can I access value from city_id?

When I use $table[0]['prices'] I get following:

[{"city_id":"304","months":"0","price":"1","minutes":"0"}]

and I don't know how to get city_id from it.

1
  • This is json, decode it. Commented Aug 23, 2017 at 7:16

1 Answer 1

1

You have JSON formatted value. Decode it using json_decode function.

//Decode JSON to object
$decoded = json_decode($table[0]['prices']);
$cityId = $decoded->city_id;

//Decode JSON to associative array
$decoded = json_decode($table[0]['prices'], true);
$cityId = $decoded['city_id'];
Sign up to request clarification or add additional context in comments.

2 Comments

I think you forgot true param for array
Yes that works perfectly, thank you guys. I will close this topic as soon i can.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.