Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中将数组作为变量传递到wp_查询循环之外_Php_Arrays_Wordpress_Loops - Fatal编程技术网

如何在php中将数组作为变量传递到wp_查询循环之外

如何在php中将数组作为变量传递到wp_查询循环之外,php,arrays,wordpress,loops,Php,Arrays,Wordpress,Loops,有没有办法将数组作为变量从wp_查询循环中传递出去?我想从这四篇文章中收集文章元——下周和接下来的三周。为了保持代码整洁,我认为捕获变量并在表单中稍后使用数据是最容易的 // Capture next week's week number $nextweek = date("W", strtotime( "+1 week")); // Query all custom post types $menu_args = array( 'post_type'=>'single_menu

有没有办法将数组作为变量从wp_查询循环中传递出去?我想从这四篇文章中收集文章元——下周和接下来的三周。为了保持代码整洁,我认为捕获变量并在表单中稍后使用数据是最容易的

// Capture next week's week number
$nextweek = date("W", strtotime( "+1 week"));

// Query all custom post types
$menu_args = array(
    'post_type'=>'single_menu',
    'posts_per_page' => -1,
    'orderby' => 'title',
    'order' => 'ASC'
    );

    $menu = new WP_Query( $menu_args ); if ( $menu->have_posts() ) : while ( $menu->have_posts() ) : $menu->the_post();

    // Get custom fields
    $menuids = array( get_post_field( 'week', $post->id ) );
    $titles = array( get_post( $post->id )->post_title ); 
    $choosefrom = array( get_post_field( 'menu_listing', $post->id ) );
    $selection = array( get_post_field( $checkout_menu, $post->id ) );

    // Create new array with custom fields  
    $result = array();
    foreach ( $menuids as $id => $menuid ) {
        $result[$menuid] = array(
            'title'  => $titles[$id],
            'offering'  => $choosefrom[$id],
            'menu' => $selection[$id],
            'delivery'    => 0,
        );
    }

    // Return the arrays for specified weeks
    for ($i = $nextweek; $i<$nextweek+4; $i++) {
    if ( $result[$menuid] == $result[$i] ) {
        print_r( $result );
    }
    }

    endwhile;   
    endif;
当我创建一个新数组时也会发生同样的情况,如下所示:

    $payload = array();
    for ($i = $nextweek; $i<$nextweek+4; $i++) {
    if ( $result[$menuid] == $result[$i] ) {

        $payload[] = $result;
    }
  }

endwhile;   
endif;

print_r( $payload );
$payload=array();

对于($i=$nextweek;$i$result,每次循环迭代都会初始化它。您的$result只包含一项。对于$payload变量,您的想法是一样的。
在while循环之外,您只有最后一项。祝您好运

有时我们无法直接看到…
$payload=array()
需要放在wp_查询之外,在
$menu_args

之前,您只是在寻找
$posts
属性吗…?我在传递从
endwhile
endif
循环中创建的数据数组时遇到了问题。
$posts
属性如何帮助我实现这一点?不是真的那么,你的问题是什么……这可能只是如何在循环中向数组中添加新元素,而不是在每次迭代中覆盖变量……?如果是这样,那么这对研究来说是非常微不足道的,好吧,谢谢你指出这应该很容易。事实证明,我的问题是我需要将
payload=array()放入
在循环之外。你帮助我停止了对它的过度思考。
    $payload = array();
    for ($i = $nextweek; $i<$nextweek+4; $i++) {
    if ( $result[$menuid] == $result[$i] ) {

        $payload[] = $result;
    }
  }

endwhile;   
endif;

print_r( $payload );