0

Excuse me if this has been covered, but I've looked and can't find an answer that works.

I have the following JSON (shortened for clarity):

[{
    "header": {
        "msg_type": "0001"
    },
    "body": {
        "event_type": "ARRIVAL",
        "train_id": "384T22MJ20"
    }
},{
    "header": {
        "msg_type": "0003"
    },
    "body": {
        "event_type": "DEPARTURE",
        "train_id": "382W22MJ19"
    }
}]

Now I know I can use json_decode to create a php array, but not knowing much about php I don't know how to get it to do what I want. I need to be able to access a value from each array. For example I would need to extract the train_id from each array. At the moment I have this:

$file = "sampleData.json";
$source = file_get_contents($file);
$data = json_decode($source);

$msgbdy = $data->body;
foreach($msgbdy as $body){
   $trainID = $body->train_id;
}

I know this is wrong, but I'm not sure if I'm on the right track or not.

Thanks in advance.

1
  • Thanks everyone, used a combination of the answers to achieve what I needed. Can't vote things up yet though. Commented Dec 30, 2014 at 23:22

5 Answers 5

2

anonymous objects are deserialized as array-members, foreach can handle that :

$objs = json_decode($source)
foreach($objs as $obj)
{
   $body = $obj->body; //this is another object!
   $header = $obj->header; //yet another object!
}

and within that loop, you can now access msg_type, train_id and event_type via $body and $header

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

Comments

1

I suggest to pass it true as a parameter, it is easier to work with associative arrays than objects:

$messages = json_decode($source, true);
foreach($messages as $message){
   $trainID = $message["body"]["train_id"];
}

1 Comment

@specializt and meda always use isset or empty. I like the use of json_decode to array.
0

You process it in the wrong order: your string is an array of objects. Not an object with arrays.

You first need the large foreach loop to iterate over the array and then access for each element the "body" and "train_id".

Your code should thus read:

foreach($data as $item) {
    echo $item->body->train_id; //or do something else with $item->body->train_id
}

Comments

0

Maybe something like:

$data = json_decode($source);

foreach ($data as $message) {
    $trainId = $message->body->train_id;
    // Not sure what you want to do wit this ^
}

You need to loop over each whole entry. See how where the data is repeated, and that is what you need to loop over.

Comments

0

Your trying to access an array like an object. The array notation would be $data[0]['body'] to retrieve the value for the "body" key in the first entry in the array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.