EvilNabster
EvilNabster

Reputation: 516

php json read file

storage.json :

{"544aee0b0a00f":{"p_name":"testname","p_about":null,"file":"images\/1.png"}}
{"548afbeb42afe":{"p_name":"testname2","p_about":null,"file":"images\/2.png"}}
{"549afc8c8890f":{"p_name":"testname3","p_about":null,"file":"images\/3.jpg"}}

Now the numbers with letters at the beginning are uniqid() function that been called when writing items to the file.

<?php
$storage_file = 'storage.json';
$storage = file_get_contents($storage_file);
$storage = json_decode($storage,true);
$storage = empty($storage) ? array() : $storage;
print_r($storage)
?>

Now i have tried to display all records from the json file but it works ONLY if i have 1 record in file, if i have more than 1 , for like here about ( 3 records ) than the result i get is simple text: Array()

Could anybody help me out ? I'm kinda stuck over here, no idea what to do to fix the issue

Upvotes: 0

Views: 173

Answers (3)

vp_arth
vp_arth

Reputation: 14982

am late, but hope, that this answer will be useful to someone.

You have valid json in each file row.
So best solution to use file():

$data = array_map(function($row){
  return json_decode($row);
}, file('storage.json'));

print_r($data);
  • file give us array of file rows(so, we don't need to explode it)
  • array_map applies json_decode to each row

For get pname's only:

$data = array_map(function($row){
  $a = json_decode($row);
  return $a[key($a)]['pname'];
}, file('storage.json'));

print_r($data);

Add
You use this code for create file:

$new_id = count($storage); 
$uid = uniqid(); 
$storage[$uid] = $new_record; 
file_put_contents($storage_file,json_encode($storage), FILE_APPEND);  

But much better to use this:

//get current database:
$data = json_decode(file_get_contents($filename), true);
//...
$uid = uniqid();
$data[$uid] = $new_record;
file_put_contents($filename, json_encode($storage));

So we always have valid json with all data.
And always can get it simple as:

//get current database:
$data = json_decode(file_get_contents($filename), true);

Upvotes: 0

meda
meda

Reputation: 45490

If you try to decode all at once it will fail due to invalid JSON because you need an array to hold multiple objects.

Instead you need to decode each line one by one:

<?php
$storage_file = 'storage.json';
$storage = file_get_contents($storage_file);
$lines = explode("\n", $storage);

for ($i=0;$i<count($lines);$i++)  
{
    $data = json_decode($lines[$i],true);
    print_r($data);
}

?>

<?php
$storage_file = 'storage.json';
$storage = file_get_contents($storage_file);
$lines = explode("\n", $storage);

foreach ($lines as $line)  
{
    $data = json_decode($line,true);
    print_r($data);
}

?>

<?php
$storage_file = 'storage.json';
$storage = file_get_contents($storage_file);
$lines = explode("\n", $storage);

foreach ($lines as $line)  
{
    $data = json_decode($line, true);
    foreach ($data as $key => $value) {
        echo "p_name = ".$data[$key]["p_name"]."\n";
    }
}

?>

Upvotes: 4

Alex
Alex

Reputation: 626

As meda mentioned above the code works perfectly fine, instead I will use foreach

$storage_file = 'storage.json';
$storage = file_get_contents($storage_file);
$lines = explode("\n", $storage);

foreach ($lines as $str){
    $data = json_decode($str,true);
    print_r($data);
}

Upvotes: 0

Related Questions