将 JSON 元素传递给另一个 JSON 对象最终变为 null【PHP】

huangapple go评论69阅读模式
英文:

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

问题

以下是您要翻译的内容:

1.json 包含未经过滤的消息
2.json 应包含经过滤的消息。

Json 格式:

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

消息过滤是在一个 PHP 文件中完成的。通过传递一个 Id,Json 元素将根据 Id 进行过滤,然后返回。但出于某种原因,整个过程返回 null 而不是实际的 Json 元素。可能是什么问题?

这是排序 PHP 文件:

<?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";
   }

这是 2.json 的输出:

[
    null,
    null
]

我通过链接传递 Id:

sorting.php?id=16

英文:

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

Json format:

    {
        &quot;Name&quot;: &quot;Test&quot;,
        &quot;Email&quot;: &quot;tesr@gmail.com&quot;,
        &quot;Comment&quot;: &quot;dsfgdfs&quot;,
        &quot;Id&quot;: 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:

&lt;?php
$subjectId  = $_GET[&#39;id&#39;];

$json = &quot;1.json&quot;;

$array = json_decode($json, true);

$result = getInfo($_GET[&#39;id&#39;], $array);

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


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

This is the output of the 2.json:

[
    null,
    null
]

I am passing the id via link:

> sorting.php?id=16

答案1

得分: 0

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 a 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 you use the provided index.php file:

$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 the provided 1.json file:

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

And if you navigate to ?id=1, ?id=2, ?id=2, ?id=1, in that order, you 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
}
]

英文:

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
}
]

huangapple
  • 本文由 发表于 2023年5月15日 01:18:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定