-4

I am new to PHP Programming, I need to parse the json string. This is the string what I have as JSON array

 [{"datetime":"17/02/2014 13:18:30","type":"testtype","locationid":"1","GPSType":"GOOGLE","GPSLatitude":"1.1","GPSLongtitude":"1.2","userid":"admin","brand":"1234567","numbers":["num1","num2","num3]}]

In the above string I need to parse numbers array only.

4
  • Use json_decode() - But make sure the JSON string is valid, else it'd return NULL. The JSON string you have in the question is not valid (as you can verify with an online validator such as jsonlint.com) Commented Feb 18, 2014 at 11:48
  • If I just search then the appropriate PHP documentation is the first hit. Commented Feb 18, 2014 at 11:49
  • sorry I checked with jsonlint.com for my json and it says valid [ { "datetime": "17/02/2014 13:18:30", "type": "testtype", "locationid": "1", "GPSType": "GOOGLE", "GPSLatitude": "1.1", "GPSLongtitude": "1.2", "userid": "admin", "brand": "1234567", "numbers": [ "num1", "num2", "num3" ] } ] Commented Feb 18, 2014 at 11:51
  • 1
    @user3323251 — There is a " in that comment that isn't in the question. Commented Feb 18, 2014 at 11:52

2 Answers 2

0

I hope you are having JSON string in your PHP code. There is an error in your JSON string. Since you are maintaining your JSON string as an array, you may follow this approach. You can understand this easily.

<?php
    $json = '[
                {
                    "datetime": "17/02/2014 13:18:30",
                    "type": "testtype",
                    "locationid": "1",
                    "GPSType": "GOOGLE",
                    "GPSLatitude": "1.1",
                    "GPSLongtitude": "1.2",
                    "userid": "admin",
                    "brand": "1234567",
                    "numbers": ["num1", "num2", "num3"]
                }
            ]';

    $obj = json_decode($json);
    $len = count($obj[0]->{'numbers'});
    for($i = 0; $i < $len; $i++) {
        echo $obj[0]->{'numbers'}[$i].'<br>';
    }
    ?>

The Output would be:

num1
num2
num3

Sign up to request clarification or add additional context in comments.

Comments

-1

As others have mentioned in the comments, use json_decode.

http://www.php.net/manual/en/function.json-decode.php

[EDIT]

In order to get only the "numbers" array, simply try this:

$json = json_decode($input, true);
$numbers = $json[0]["numbers"];

The json_decode function will convert your JSON string to an associative array, from which you can just get the indices you need.

3 Comments

Please avoid link only answers. Answers that are "barely more than a link to an external site” may be deleted.
Please read the first half of the answer and ask yourself: is this a "link only answer"?
The answer provides all the information the OP has requested: a way to parse JSON strings in PHP. Plus a link to the documentation. I fail to see the problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.