函数与调用之间的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?

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

You add to $final inside a function, but unlike javascript, php doesn't know about global variables unless you "include" them in your function with the keyword global or when you pass them as a parameter (which I prefer)