如何在PHP中的JSON对象中提取对象并为其分配正确的索引

时间:2018-09-17 05:51:27

标签: php json

我有以下PHP代码(我将发布其中的重要部分):

 // objID
 $objects->objID = generateRandomID();

 $objects->pointer = array('type'=>'__pointer','objID'=>'dgFg45dG','className'=>'Users');

$jsonStr = file_get_contents($className.'.json'); // This calls a Users.json file stored in my server
$jsonObjs = json_decode($jsonStr, true);

            ...

$jsonStr = file_get_contents($className.'.json'); // This calls a Users.json file stored in my server
$jsonObjs = json_decode($jsonStr, true);

array_push($jsonObjs, $objects);

// Encode the array back into a JSON string and save it.
$jsonData = json_encode($jsonObjs);
file_put_contents($className.'.json', $jsonData);
// echo JSON data
echo $jsonData;


    // ISSUE HERE :(
    $jsonStr = file_get_contents($className.'.json');
    // Decode the JSON string into a PHP array.
    $jsonObjs = json_decode($jsonStr, true);

    foreach($jsonObjs as $i=>$obj) {
        print_r('<br><br>'.$i.'-- ');
        echo 
            $obj['objID'].', <br>'
            .$obj['pointer']["$i"]['objID']. ', '
            .$obj['pointer']["$i"]['type']. ', '
            .$obj['pointer']["$i"]['className']. '<br><br>'
        ;               
    }
    // ./ ISSUE 

上面的代码在我自己的Users.json文件中创建了一个新的JSON对象。 因此,当我在浏览器中使用URL字符串调用此PHP文件(作为测试),并刷新页面几次时,得到以下回显:

0-- VUDjCZX8QX, , , 

1-- 1uWH17OoJP, , , 

[{"objID":"VUDjCZX8QX","pointer":{"type":"__pointer","objID":"dgFg45dG","className":"Users"},"string":"mark","createdOn":"2018-09-17 05:36:49","updatedOn":"2018-09-17 05:36:49","number":111,"boolean":true,"array":["john","sarah"]},{"objID":"1uWH17OoJP","pointer":{"type":"__pointer","objID":"dgFg45dG","className":"Users"},"string":"mark","createdOn":"2018-09-17 05:36:51","updatedOn":"2018-09-17 05:36:51","number":111,"boolean":true,"array":["john","sarah"]},{"objID":"RkubyQPvqR","pointer":{"type":"__pointer","objID":"dgFg45dG","className":"Users"},"string":"mark","createdOn":"2018-09-17 05:36:54","updatedOn":"2018-09-17 05:36:54","number":111,"boolean":true,"array":["john","sarah"]}]

因此,我基本上需要解决的问题如下:

  1. 正确获取代码清单的正确代码是什么 是Users.json文件中每个对象内的“指针”对象?
  2. 我尝试跟踪foreach循环的索引,但是如上首次执行PHP代码时上面发布的echo所示,它无法正常工作,我得到了JSON我的第一个对象的字符串,我没有得到任何print_r()。然后,当我第二次刷新页面时,得到了第一个对象的objID字符串的打印,再次,如果我第三次刷新了页面,则得到了我的objID第二个对象,而我的json文件中存储了3个对象。依此类推,换句话说,我从来没有得到第一个对象的打印信息。

我在做什么错了?

2 个答案:

答案 0 :(得分:1)

您将$i作为字符串而不是变量传递。使用双引号(")或删除单引号(')作为变量传递。这将解决您的问题,指针对象无法正确打印。

$obj['pointer'][$i]['objID']

更新

[{"objID":"VUDjCZX8QX","pointer":{"type":"__pointer","objID":"dgFg45dG","className":"Users"},"string":"mark","createdOn":"2018-09-17 05:36:49","updatedOn":"2018-09-17 05:36:49","number":111,"boolean":true,"array":["john","sarah"]},{"objID":"1uWH17OoJP","pointer":{"type":"__pointer","objID":"dgFg45dG","className":"Users"},"string":"mark","createdOn":"2018-09-17 05:36:51","updatedOn":"2018-09-17 05:36:51","number":111,"boolean":true,"array":["john","sarah"]},{"objID":"RkubyQPvqR","pointer":{"type":"__pointer","objID":"dgFg45dG","className":"Users"},"string":"mark","createdOn":"2018-09-17 05:36:54","updatedOn":"2018-09-17 05:36:54","number":111,"boolean":true,"array":["john","sarah"]}]

根据上述JSON字符串,您无需指定$i

$obj['pointer']['objID']应该起作用,因为它是关联数组。

答案 1 :(得分:0)

由于@ saumini-navaratnam,我必须使用以下foreach

foreach($jsonObjs as $i=>$obj) {
   print_r('<br><br>'.$i.'-- ');
   echo 
       $obj['objID'].', '
       .$obj['pointer']['objID']. ', '
       .$obj['pointer']['type']. ', '
       .$obj['pointer']['className']. '<br><br>'
   ;               
} 

通过这种方式,我可以正确地获得该对象的对象:

{"pointer":{"type":"__pointer","objID":"dgFg45dG","className":"Users"}

实际上,这是我得到的回声:

 [
{"objID":"pkO8NesS5S","pointer":{"type":"__pointer","objID":"dgFg45dG","className":"Users"},"string":"bobby","createdOn":"2018-09-17 07:03:27","updatedOn":"2018-09-17 07:03:27","number":111,"boolean":true,"array":["john","sarah"]},
{"objID":"rdwJl20krC","pointer":{"type":"__pointer","objID":"dgFg45dG","className":"Users"},"string":"bobby","createdOn":"2018-09-17 07:03:31","updatedOn":"2018-09-17 07:03:31","number":111,"boolean":true,"array":["john","sarah"]},
{"objID":"3WspzmuwMK","pointer":{"type":"__pointer","objID":"dgFg45dG","className":"Users"},"string":"bobby","createdOn":"2018-09-17 07:07:39","updatedOn":"2018-09-17 07:07:39","number":111,"boolean":true,"array":["john","sarah"]}
]

0-- pkO8NesS5S, dgFg45dG, __pointer, Users


1-- rdwJl20krC, dgFg45dG, __pointer, Users


2-- 3WspzmuwMK, dgFg45dG, __pointer, Users