Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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输出中获得这种格式,但似乎不知道如何降低格式 { "total": 20, "result": [ { "image": "http://test.com/images/001.jpg", "width": 192, "height": 288 }, { "image": "http://test.com/images/001.jpg", "width": 192,

我试图在我的jSON输出中获得这种格式,但似乎不知道如何降低格式

{
"total": 20,
"result": [
    {
        "image": "http://test.com/images/001.jpg",
        "width": 192,
        "height": 288
    },
    {
        "image": "http://test.com/images/001.jpg",
        "width": 192,
        "height": 257
    },
    {
        "image": "http://test.com/images/001.jpg",
        "width": 192,
        "height": 288
    },
    {
        "image": "http://test.com/images/001.jpg",
        "width": 192,
        "height": 288
    }
]
}
我能够做到这一点,但不包括总数:

$json['result'][] = array('image' => 'http://www.google.com', 'width' => '120', 'height' => '200');
json_encode($json);
我的最终目标是能够从这个数据库调用中创建json输出:

$objDB = new DB;
    $submissions = $objDB
    ->setStoredProc( 'petContestFetchImages' )
    ->setParam("offset", $offset )
    ->setParam("rows", $limit )
    ->execStoredProc()
    ->parseXML(); 

    //$link = $submissions->link; 
    //$width = $submissions->width;
    //$heigth = $submissions->height; 
    //$total = $submissions->total;
编辑

这是我的数据库尝试,但是jSON响应似乎关闭了

//Fetch Entries
$objDB = new DB;
    $submissions = $objDB
    ->setStoredProc( 'petContestFetchImages' )
    ->setParam("offset", $offset )
    ->setParam("rows", $limit )
    ->execStoredProc()
    ->parseXML();     

//Total
$json['total'] = $submissions->total;   

//Loop over the result set and store it as jSON
foreach($submissions->submission as $submission) { 

    $json['result'][] = array('petName' => $submission->petName, 'petCaption' => $submission->petCaption, 'imageName' => $submission->imageName);   

}

//Output the result
print json_encode($json);
输出:

{

   "total": 20,

   "result": [

      {

         "petName": {

            "0": "Nala"

         },

         "petCaption": {

            "0": "Shes a wonder pup!"

         },

         "imageName": {

            "0": "nalaHUS123.png"

         }

      },

      {

         "petName": {

            "0": "Simba"

         },

         "petCaption": {

            "0": "Shes a wonder pup!"

         },

         "imageName": {

            "0": "simbaHUS123.png"

         }

      },

      {

         "petName": {

            "0": "BoBo"

         },

         "petCaption": {

            "0": "Shes a wonder pup!"

         },

         "imageName": {

            "0": "boboHUS123.png"

         }

      },

      {

         "petName": {

            "0": "Frank"

         },

         "petCaption": {

            "0": "Shes a wonder pup!"

         },

         "imageName": {

            "0": "frankHUS123.png"

         }

      },

      {

         "petName": {

            "0": "Jim"

         },

         "petCaption": {

            "0": "Shes a wonder pup!"

         },

         "imageName": {

            "0": "frankHUS123.png"

         }

      },

      {

         "petName": {

            "0": "Sasha"

         },

         "petCaption": {

            "0": "Shes a wonder pup!"

         },

         "imageName": {

            "0": "boboHUS123.png"

         }

      },

      {

         "petName": {

            "0": "Sam"

         },

         "petCaption": {

            "0": "Shes a wonder pup!"

         },

         "imageName": {

            "0": "simbaHUS123.png"

         }

      },

      {

         "petName": {

            "0": "Alice"

         },

         "petCaption": {

            "0": "Shes a wonder pup!"

         },

         "imageName": {

            "0": "nalaHUS123.png"

         }

      },

      {

         "petName": {

            "0": "Sara"

         },

         "petCaption": {

            "0": "Shes a wonder pup!"

         },

         "imageName": {

            "0": "boboHUS123.png"

         }

      },

      {

         "petName": {

            "0": "Judy"

         },

         "petCaption": {

            "0": "Shes a wonder pup!"

         },

         "imageName": {

            "0": "nalaHUS123.png"

         }

      }

   ]

}

您需要创建一个数组,其中包含总计字符串和一个结果数组

$json[] = array('image' => 'http://www.google.com', 'width' => '120', 'height' => '200');
$result = array('total'=>20,'result'=>$json);

json_encode($result);

首先从数据库中获取数据。这可能不会准确地输出您需要的JSON,但这将为您提供解决方案

$result = array();
while($row = mysli_fetch_assoc($query))
{
$result[] = array('image'=>$row['image'],'height',=>$row['height'],'width'=>$row['width']);
}
$data =  array( 'total'=>count($result),'result'=>$result);
json_encode($data);

我尝试了一些类似的东西,但是输出有点差。增加原有职位
$result = array();
while($row = mysli_fetch_assoc($query))
{
$result[] = array('image'=>$row['image'],'height',=>$row['height'],'width'=>$row['width']);
}
$data =  array( 'total'=>count($result),'result'=>$result);
json_encode($data);