Keerati Pongram
Keerati Pongram

Reputation: 71

JSON_DECODE for each php not working

This is my sample.txt

{ "Messages": [ { "address": "AIS", "body": "3989เพื่อป้องกันมิจฉาชีพอย่าบอกรหัสผู้ใด AIS จะไม่ถามรหัสจากคุณ", "date": "1436164374077", "read": "0", "msgtype": "inbox" }, { "address": "+66819129634", "body": "7778", "date": "1436090922311", "read": "0", "msgtype": "inbox" }, { "address": "+66819129634", "body": "7778", "date": "1436090922132", "read": "1", "msgtype": "sent" } ] }

<?php
$jsondata = file_get_contents("sample.txt");
$json = json_decode($jsondata, true);
?>
<ul>
<?php foreach($json['Messages'] as $message) : ?>
<li><?php echo $message['address']; ?></li>
<li><?php echo $message['body']; ?></li>
<li><?php echo $message['data']; ?></li>
<li><?php echo $message['read']; ?></li>
<li><?php echo $message['msgtype']; ?></li>
<?php endforeach; ?>
</ul>

I don't know why is not working

i think it issue about my foreach

Upvotes: 0

Views: 168

Answers (4)

Pratik Soni
Pratik Soni

Reputation: 2588

Fix $message['data'] to $message['date']. I think the issue is with array index.

And i assume that it is being converted in Stdclass Object.

If you are not able to see errors then you may use.

ini_set('display_errors',1);
error_reporting(E_ERROR | E_WARNING | E_PARSE);

You can try this.

<ul>
<?php foreach($json->Messages as $message) : ?>
<li><?php echo $message->address; ?></li>
<li><?php echo $message->body; ?></li>
<li><?php echo $message->data; ?></li>
<li><?php echo $message->read; ?></li>
<li><?php echo $message->msgtype; ?></li>
<?php endforeach; ?>
</ul>

Upvotes: 2

kevin
kevin

Reputation: 300

i suggest you to use curl because file get content is not secure below is code that may help you

function get_content($URL){
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_URL, $URL);
          $data = curl_exec($ch);
          curl_close($ch);
          return $data;
      }


 echo get_content('http://example.com');

Upvotes: 0

Syerad
Syerad

Reputation: 113

This worked for me:

$jsondata = file_get_contents($path);
$json = json_decode($jsondata, true);

        foreach($json['Messages'] as $message){
             echo $message['address'];
             echo $message['body'];
             echo $message['date'];
             echo $message['read'];
             echo $message['msgtype'];
        }

There was a problem with the array-index:

echo $message['data'];

'data' is not an array key, it is 'date'

Hope it helps

Upvotes: 0

Jagadeesh
Jagadeesh

Reputation: 754

use the following so that it will give the results

<?php foreach($json['Messages'] as $message) : ?>
<ul>
<li><?php echo $message['address']; ?></li>
<li><?php echo $message['body']; ?></li>
<li><?php echo $message['date']; ?></li>
<li><?php echo $message['read']; ?></li>
<li><?php echo $message['msgtype']; ?></li>
</ul>
<?php endforeach; ?>

and the result will be

AIS
3989เà,žà¸·à¹ˆà¸­à¸›à¹‰à¸­à¸‡à¸à¸±à¸™à¸¡à¸´à¸ˆà¸‰à¸²à¸Šà¸µà¸žà¸­à¸¢à¹ˆà¸²à¸šà¸­à¸à¸£à¸«à¸±à¸ªà¸œà¸¹à¹‰à¹ƒà¸” AIS จะไม่ถามรหัสจาà¸à¸„ุณ
1436164374077
0
inbox

+66819129634
7778
1436090922311
0
inbox

+66819129634
7778
1436090922132
1
sent

Upvotes: 0

Related Questions