如何在PHP中使用array_push向数组中添加额外的元素?

时间:2015-07-11 19:26:38

标签: php json

我正在开发PHP / MS SQL以获取JSON响应。

我写的代码是:

[{"idperiodo":"37","idgrupo":"1963","idhorario":"12832","unidades":null,"unidad":1}]

这就是我想要的结果:

[{"idperiodo":"37","idgrupo":"1963","idhorario":"12832","unidades":null},{"unidad":1}]

但结果显示了这一点:

<script src="https://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
// Load the Search API
google.load('search', '1');

// Set a callback to load the Custom Search Element when you page loads
google.setOnLoadCallback(
  function(){
    var customSearchControl = new google.search.CustomSearchControl('011927268382499158334:cuvurabmo-o');

    // Use "mysite_" as a unique ID to override the default rendering.
    google.search.Csedr.addOverride("mysite_");

    // Draw the Custom Search Control in the div named "CSE"
    customSearchControl.draw('cse');

  },
true);
</script>

1 个答案:

答案 0 :(得分:2)

您正在抓取一个对象。首先将$uni添加到$result,然后添加到$array_res

while( $result = sqlsrv_fetch_object($sql_Gpo_Carr)) {
    $result->unidad = $uni;
    $array_res[] = $result;
}

此外,您可能希望循环后的json_encode()不在循环中:

echo json_encode($array_res);