Search code examples
phpjsonfilterelement

Passing json element to another json object ends up in null [php]


I have two json files (1.json and 2.json). 1.json contains unfiltered messages 2.json should contain filtered messages.

Json format:

    {
        "Name": "Test",
        "Email": "tesr@gmail.com",
        "Comment": "dsfgdfs",
        "Id": 16
    },

Filtering messages is done in a php file. When passing an Id, the json element is filtered by Id and then returned. However, for some reason, the whole thing returns null instead of an actual json element. What could be the issue?

Here's the sorting php file:

<?php
$subjectId  = $_GET['id'];

$json = "1.json";

$array = json_decode($json, true);

$result = getInfo($_GET['id'], $array);

function getInfo($id, $array) {
    foreach($array AS $index => $element) {
        if($element["Id"] == $id) {
            return $element;
        
        }
    }
}


   if(filesize("2.json") == 0){
      $first_record = array($result);
      $data_to_save = $first_record;
   }else{
      $old_records = json_decode(file_get_contents("2.json"));
      array_push($old_records, $result);
      $data_to_save = $old_records;
   }
   
    $encoded_data = json_encode($data_to_save, JSON_PRETTY_PRINT);
 
   if(!file_put_contents("2.json", $encoded_data, LOCK_EX)){
      $error = "Error storing message, please try again";
   }else{
      $success =  "Message is stored successfully";
   }

This is the output of the 2.json:

[
    null,
    null
]

I am passing the id via link:

sorting.php?id=16


Solution

  • I notice that you're passing the name of the file into json_decode and not the file contents.

    $json = "1.json";
    $array = json_decode($json, true);
    

    You would want to pass the file contents instead, with something like:

    $json = file_get_contents("1.json");
    $array = json_decode($json, true);
    

    Also, what does the entire 1.json file look like? Is $array actually being populated or is it NULL?

    If the JSON is a collection of an ordered list of key-value pairs separated by comma (,) and enclosed by a pair of square brackets [...], then 1.json might look like this for example:

    [
        {
        "Name": "Test1",
        "Email": "test1@gmail.com",
        "Comment": "test1",
        "Id": 1
        },
        {
        "Name": "Test2",
        "Email": "test2@gmail.com",
        "Comment": "test2",
        "Id": 2
        }
    ]
    

    When you json_decode the contents of this 1.json file into $array with json_decode($json, true) for example, you would end up with an array of arrays:

    array(2) {
      [0]=>
      array(4) {
        ["Name"]=>
        string(4) "Test1"
        ["Email"]=>
        string(14) "test1@gmail.com"
        ["Comment"]=>
        string(7) "test1"
        ["Id"]=>
        int(1)
      }
      [1]=>
      array(4) {
        ["Name"]=>
        string(5) "Test2"
        ["Email"]=>
        string(15) "test2@gmail.com"
        ["Comment"]=>
        string(5) "test2"
        ["Id"]=>
        int(2)
      }
    }
    

    When I use this index.php file:

    <?php
    $subjectId  = $_GET['id'];
    
    $json = file_get_contents("1.json");
    
    $array = json_decode($json, true);
    
    $result = getInfo($_GET['id'], $array);
    
    function getInfo($id, $array)
    {
        foreach ($array as $index => $element) {
            if ($element["Id"] == $id) {
                return $element;
            }
        }
    }
    
    if (filesize("2.json") == 0) {
        $first_record = array($result);
        $data_to_save = $first_record;
    } else {
        $old_records = json_decode(file_get_contents("2.json"));
        array_push($old_records, $result);
        $data_to_save = $old_records;
    }
    
    $encoded_data = json_encode($data_to_save, JSON_PRETTY_PRINT);
    
    if (!file_put_contents("2.json", $encoded_data, LOCK_EX)) {
        $error = "Error storing message, please try again";
    } else {
        $success =  "Message is stored successfully";
    }
    

    With this 1.json file:

    [
        {
        "Name": "Test",
        "Email": "test1@gmail.com",
        "Comment": "test1",
        "Id": 1
    },
    {
        "Name": "Test2",
        "Email": "test2@gmail.com",
        "Comment": "test2",
        "Id": 2
    }]
    

    And I navigate to ?id=1, and ?id=2, ?id=2, ?id=1, in that order, for example, I get a resulting 2.json file like this:

    [
        {
            "Name": "Test",
            "Email": "test1@gmail.com",
            "Comment": "test1",
            "Id": 1
        },
        {
            "Name": "Test2",
            "Email": "test2@gmail.com",
            "Comment": "test2",
            "Id": 2
        },
        {
            "Name": "Test2",
            "Email": "test2@gmail.com",
            "Comment": "test2",
            "Id": 2
        },
        {
            "Name": "Test",
            "Email": "test1@gmail.com",
            "Comment": "test1",
            "Id": 1
        }
    ]