Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要在php中的单个文档中创建多个json对象_Php_Json - Fatal编程技术网

需要在php中的单个文档中创建多个json对象

需要在php中的单个文档中创建多个json对象,php,json,Php,Json,我已经使用这段代码创建了json对象 if(mysql_num_rows($result)) { while($document = mysql_fetch_assoc($result)) { $documents[] = $document; } } header('Content-type: application/json'); echo json_encode(array('documents'=>$documents)); 它在这样的文

我已经使用这段代码创建了
json
对象

if(mysql_num_rows($result)) {
    while($document = mysql_fetch_assoc($result)) {
      $documents[] = $document;
        }
  }

header('Content-type: application/json');
echo json_encode(array('documents'=>$documents));
它在这样的文档中为我提供了一个json

{"document":[{"document":{"id":"2","name":"test","pages":"name","img":"path","lines":"data"}]}
但我需要

{"document":[{"document":{"id":"2","name":"test",pages":[{"img":"first img","lines":" <p>first line"}],pages":[{"img":"second img","lines":" <p>second line"}]}]}
{“document”:[{“document”:{“id”:“2”,“name”:“test”,pages:[{“img”:“first img”,“line”:“first line”}],pages:[{“img”:“second img”,“line”:“second line”}]
意味着在现有的json中需要另一个json名称页面,其中包含更多的img和行。
它是如何做到的?

如果您这样做:

if(mysql_num_rows($result)) {
    while($document = mysql_fetch_assoc($result)) {
      $document["pages"] = array();
      $document["pages"][] = array("img" => "first img", "lines" => "first line");
      $document["pages"][] = array("img" => "second img", "lines" => "second line");
      $documents[] = $document;
    }
}
{"document": [{"document" : {"id":"2",
                             "name":"test", 
                             "pages":[{"img":"first img","lines":" <p>first line"},
                                     {"img":"second img","lines":" <p>second line"}]
                            }}
              ]}
json_encode(array('documents'=>$documents));
将返回json对象,其中每个文档将包含一个包含字段
img
行的页面数组。基本上,您只需在PHP中创建所需的结构,就可以在json中获得相同的结构

上面的代码应该返回如下内容:

if(mysql_num_rows($result)) {
    while($document = mysql_fetch_assoc($result)) {
      $document["pages"] = array();
      $document["pages"][] = array("img" => "first img", "lines" => "first line");
      $document["pages"][] = array("img" => "second img", "lines" => "second line");
      $documents[] = $document;
    }
}
{"document": [{"document" : {"id":"2",
                             "name":"test", 
                             "pages":[{"img":"first img","lines":" <p>first line"},
                                     {"img":"second img","lines":" <p>second line"}]
                            }}
              ]}
{“document”:[{“document”:{“id”:“2”,
“名称”:“测试”,
“页面”:[{“img”:“第一个img”,“行”:“第一行”},
{“img”:“第二img”,“行”:“secondline”}]
}}
]}

“在现有的json中,意味着需要另一个包含更多img和行的json名称页。它可以做什么?”很抱歉,但这在英语中没有任何意义。你能再试一次吗(也许向同事寻求帮助?)。因此,在使用echo之前,请使您的数组符合您的需要。。然后使用json_encode对其进行echo。。我是新手。如何在回答的开头创建php structure.php代码。它显示了如何创建与您需要的结构相似的结构。在您的情况下,应如何创建它-取决于您的项目。