PHP在foreach之后返回NULL数组


PHP return array as NULL after foreach

我不知道为什么我的函数总是返回NULL。return之前的var_dump($args)显示了$args中的过多项。但是当我在另一个地方调用这个函数时,它返回NULL。有人能帮帮我吗?谢谢。

在备忘录中我解释了情况。

function LBE_result_hompage_search_ecole_map()
{
    //  Get department code when user click map
    $code = $_POST['code'];
    // Query data from database
    global $wpdb;
    $table_name = $wpdb->prefix.'utils_ville';
    $query = "SELECT nom_ville,code_postal FROM ".$table_name." WHERE departement=%d";
    $results = $wpdb->get_results( $wpdb->prepare($query, $code) );
    $args = array();
    foreach($results as $result):
        $arg = array(
            'posts_per_page'   => 10,
            'orderby'          => 'post_date',
            'order'            => 'DESC',
            'post_type'        => 'ecole',
            'post_status'      => 'publish',
            'meta_query'       => array(
            'relation' => 'AND',
                array(
                    'key' => 'ecole_ville',
                    'value' => $result->nom_ville,
                    'compare' => '='
                ),
                array(
                    'key' => 'ecole_post',
                    'value' => $result->code_postal,
                    'compare' => '='
                )
            )
        );
        $args[] = $arg;
    endforeach;
    var_dump($args);  // Here shows so many items in $args
    return($agrs);
}
$args = LBE_result_hompage_search_ecole_map();
var_dump($args);  // Here shows NULL. I don't know why...

返回语句中有一个错别字。

return($agrs);应为return($args);

return不是函数。还有,你要用agrs而不是args

return $args;改变return($agrs);

"Note: Note that since return is a language construct and not a function, the parentheses surrounding its arguments are not required. It is common to leave them out, and you actually should do so as PHP has less work to do in this case."

http://php.net/manual/en/function.return.php