dpruwm6206 2014-10-21 14:35
浏览 35
已采纳

函数与调用之间的push_array

At the top of document I declared

$final = array();

Then I got the following functions

function delivery($optional = null){

   global $db;
   //$return = $_POST;
   if(!empty($optional['reportdate_from'])) {
    //$return1['from'] = $optional['reportdate_from'];
   }
   if(!empty($optional['reportdate_to'])) {
    //$return1['from'] = $optional['reportdate_from'];
   }
   $sth = $db->prepare("SELECT  DATE(date) Date, COUNT(number) totalCOunt FROM numbers GROUP BY  DATE(date)");
   $sth->execute();
   $result = $sth->fetchAll();
            $return1['name'] = 'W doręczeniu';
            foreach ($result as $row1) {
            $date1 =strtotime($row1['Date'])*1000;
            $return1['data'][] = array($date1, (int)$row1['totalCOunt']);
            }
return array_push($final,$return1);
// echo json_encode($final,true);

}
function delivered($optional){
   global $db;
   $return = $_POST;
   if(!empty($optional['reportdate_from'])) {
    //$return1['from'] = $optional['reportdate_from'];
   }
   if(!empty($optional['reportdate_to'])) {
    //$return1['to'] = $optional['reportdate_to'];
   }
   $sth = $db->prepare("SELECT  DATE(date) Date, COUNT(number) totalCOunt FROM numbers Where `return` = 1 AND rdate != '0000-00-00 00:00:00' GROUP BY  DATE(date)");
   $sth->execute();
   $result = $sth->fetchAll();

            $return1['name'] = 'Doręczone';
            foreach ($result as $row1) {
            $date1 =strtotime($row1['Date'])*1000;
            $return1['data'][] = array($date1, (int)$row1['totalCOunt']);
            }

return array_push($final,$return1);

}

and at the very bottom of the document I have got:

echo json_encode($final,true);

The overall output is [] No Properties And Warning: array_push() expects parameter 1 to be array, null given in

All the arrays are not null it only happens if they are array_push(ed)

Calling the functions.

 case "test": delivery($optional); break;
 case "test1": delivered($optional); break;

Why is this error occuring?

  • 写回答

2条回答 默认 最新

  • dongmouhao7438 2014-10-21 14:38
    关注

    You never did global $final inside your functions, so the global $final you defined as an array is out-of-scope/invisible/non-existent inside your function.

    return array_push($final,$return1);
    

    This line will create a NEW local $final, push $return1 on onto it, and return that new array to the calling context. And since you don't CAPTURE that return value anywhere, the array then simply gets tossed into the garbage.

    If you had something like this:

    case "test":  array_push($final, delivery($optional)); break;
    

    then your code would have worked

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接