在同一循环中将一个数组附加到另​​一个数组 [英] Appending one array to another array in the same loop

查看:96
本文介绍了在同一循环中将一个数组附加到另​​一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是针对自定义Wordpress页面的,但是我认为基本数组原则应该适用.我以前没有使用过复杂的阵列,所以有点迷失了,尝试和错误还没有奏效.

This is for a custom Wordpress page but I think the basic array principles should apply. I've not worked with complex arrays before so am a little lost, trial and error hasn't worked yet.

我有一个帖子数据库,每个帖子都有"shop"和"expired"的meta_key. 过期"是一个日期(YYYY-MM-DD),用于告诉访问者帖子的内容何时过期,并且此密钥是我尝试使用的密钥.

I have a database of Posts, each post has meta_key's of 'shop' and 'expired'. 'expired' is a date (YYYY-MM-DD) which is used to tell the visitor when a Post's content expires and this key is what I'm trying to work with.

如果帖子的过期"日期在今天之前,则显示为优惠已过期"

If a Post's 'expired' date is before today, they are shown 'Offer expired'

如果过期"日期是将来的日期,则显示为"X天内提供优惠"(此脚本未在下面显示,没有必要)

If the 'expired' date is in the future, they are shown 'Offer expires in X days' (this script isn't shown below, not necessary)

帖子按其过期"日期ASC的顺序列出.问题是,当帖子过期时,我希望该帖子显示在末尾而不是停留在顶部.

Posts are listed in order of their 'expired' date, ASC. The problem is that when a post expires I'd like that post to show at the end rather than stay on top.

我目前看到的例子:

Post 1 | Expired 3 days ago
Post 2 | Expired 1 day ago
Post 3 | Expires in 2 days
Post 4 | Expires in 6 days

以及我想看到的内容(请注意Post X顺序):

And what I'd like to see (note Post X order):

Post 3 | Expires in 2 days
Post 4 | Expires in 6 days
Post 2 | Expired 1 day ago
Post 1 | Expired 3 days ago

这是我尝试将两者合并的数组代码

This is my array code where I've attempted to merge the two

$postid = get_the_ID();
$meta1 = get_post_meta($postid, 'shop', true);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$today = date('Y-m-d', time());

$args = array(
'post_type' => 'post',
'meta_query' => array(
    array(
        'key' => 'shop',
        'value' => $meta1
    )
),
'paged' => $paged,
'posts_per_page' => '5',
'meta_key' => 'expired',
'meta_value' => $today,
'meta_compare' => '>',
'orderby' => 'meta_value',
'order' => 'ASC'
 );

$args2 = array(
'post_type' => 'post',
'meta_query' => array(
    array(
        'key' => 'shop',
        'value' => $meta1
    )
),
'meta_key' => 'expired',
'meta_value' => $today,
'meta_compare' => '<',
'orderby' => 'meta_value',
'order' => 'DESC'
);

$final = $args + $args2;
$query = new WP_Query( $final );

while ( $query->have_posts() ) : $query->the_post(); ?>
HTML FOR DISPLAYING POST
endwhile;

此刻,它似乎并没有注意到"$ args2",仅显示$ args

At the moment it doesn't seem to take any notice of "$args2" and only displays $args

我确定我的想法是正确的,需要创建两个数组,并使用"+"而不是array_merge()将它们连接起来,但这是我无法得到的.

I'm sure my idea is on the right lines, needing to create two arrays and join them with the "+" rather than array_merge() but that's where I can't get any further.

请有人能给我一些启示吗?谢谢!

Can someone kindly shed some light please? Thanks!

推荐答案

现在,如果我正确地理解了您的要求,那么您尝试实现的解决方案实际上是不可能的.让我解释一下为什么这是无法实现的.

Now the solution you are trying to achieve is actually impossible if i understood your requirement properly. Let me explain why this is not achievable.

在您的两个数组$args$args2中,大多数值相同,只剩下两个赔率,我只选择一个来说明:

In your two arrays $args and $args2 most of the values are same leaving two odds , i am picking only one to just illustrate :

//it's in args    
'meta_compare' => '>'
//it's in args2
'meta_compare' => '<'

现在,当您尝试使用 array_merge($ args ,$ args2):

'meta_compare' => '<'

这意味着它将从后面的数组$args2中获取'meta_compare'.这是在文档中定义的array_merge函数的行为:

That means it is taking 'meta_compare' from the later array which is $args2 here. This is the behavior of array_merge function defined in the doc:

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one

现在,如果您使用的是+数组联合运算符$args+$args2:

Now if you are using + array union operator $args+$args2 :

'meta_compare' => '>'

这意味着它将从第一个数组$args中获取'meta_compare'.这是doc中定义的+数组联合运算符的行为:

That means it is taking 'meta_compare' from the first array which is $args here. This is the behavior of + array union operator defined in the doc :

The keys from the first array will be preserved. If an array key exists in both arrays, then the element from the first array will be used and the matching key's element from the second array will be ignored.

为什么会这样,因为在同一级别, 键必须唯一 .因此,在您的情况下,它们处于同一级别:

Why it is happening because in the same level keys have to be unique . So in your situation they are in the same level :

$args = array ('key' => 'value1')
$args2= array ('key' => 'value2')

因此,仅 只能存在一个 值,因为在合并数组'key'中可以指向 仅一个 .

So only and only one value can exist here as in the merged array 'key' can point only one.

如果是这种情况:

$args [0] = array ('key' => 'value1' ) 
$args2 [1]= array ('key' => 'value2' ) 

然后key存储的两个值都将被保存.这将是结果数组:

Then both of the value stored by key will be resrved. This will be the resulting array :

array(2) {
  [0]=>
  array(1) {
    ["key"]=>
    string(6) "value1"
  }
  [1]=>
  array(1) {
    ["key"]=>
    string(6) "value2"
  }
}

尽管我不是WP的极客,但据我从 WP_query 的了解,您可以不能传递这样的args( 我不确定 ).因此,这仅是实现此目的的一种方法.您可以分别传递这两个参数,然后合并结果,因为结果很可能( 我不确定 )将是一个2D数组,可以轻松合并它们.

Though i am not a geek of WP but as far as i understood from WP_query you can't pass args like this (I am not sure). So that leaves only one way to achieve this. You can pass these two args separately and merge the result as the result most probably (I am not sure) will be a 2D array you can merge them easily.

希望有帮助. 编码愉快:)

Hope that helps. Happy coding :)

这篇关于在同一循环中将一个数组附加到另​​一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆