Don P
Don P

Reputation: 63728

PHP: Parse JSON and square brackets

Using PHP, how I can I parse a JSON object with an array given in square brackets and delimited by a pipe "|"?

For example, I need to access the second component of the "Data", in this case "English". Assume the lengths are variable, so you cannot slice by character positions.

{"Username":"5018",
 "Data":["53094185|English|USA|2012-07-24 12:49:00|AZ|"],
 "Location":"New York"}

Upvotes: 1

Views: 1887

Answers (2)

MaX
MaX

Reputation: 1344

Its simple:

$X = json_decode($JSON);
$arr = explode('|', $X->Data[0]);
print_r($arr)

Upvotes: 2

Mike Brant
Mike Brant

Reputation: 71422

json_decode it. You will still need to split up the data array value string as this is just a simple string in JSON.

Upvotes: 0

Related Questions