在foreach之后PHP返回数组为NULL

时间:2014-07-29 15:46:31

标签: php arrays foreach null return

我不知道为什么我的函数总是返回NULL。返回前的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...

2 个答案:

答案 0 :(得分:2)

你的退货声明中有一个拼写错误。

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

答案 1 :(得分:0)

return不是一个功能。另外,你把agrs不是args。

使用return($agrs);

更改return $args;

"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