繁体   English   中英

PHP-如何将数组附加到具有特定键和值的另一个数组中?

[英]PHP - How to append an array into another array with specific key and value?

我不可能,但是我有两个数组。 数组一是这样的:

     Array ( [0] => link
             [1] => link
             [2] => link 
             [3] => link

我有另一个数组:

Array ( [0] => WP_Post Object ( [ID] => 83 [post_author] => 1 [post_date] => 2014-03-05 16:30:45 [post_date_gmt] => 2014-03-05 16:30:45)
        [1] => WP_Post Object ( [ID] => 33 [post_author] => 1 [post_date] => 2014-03-05 16:30:45 [post_date_gmt] => 2014-03-05 16:30:45)
        [2] => WP_Post Object ( [ID] => 34 [post_author] => 1 [post_date] => 2014-03-05 16:30:45 [post_date_gmt] => 2014-03-05 16:30:45)
        [3] => WP_Post Object ( [ID] => 80 [post_author] => 1 [post_date] => 2014-03-05 16:30:45 [post_date_gmt] => 2014-03-05 16:30:45) )

是否有可能每个第一数组的值的与特定键添加到第二阵列:例如:

Array (     [0] => WP_Post Object ( [ID] => 83 [post_author] => 1 [post_date] => 2014-03-05 16:30:45 [post_date_gmt] => 2014-03-05 16:30:45 [post_permalink] => link)
            [1] => WP_Post Object ( [ID] => 33 [post_author] => 1 [post_date] => 2014-03-05 16:30:45 [post_date_gmt] => 2014-03-05 16:30:45 [post_permalink] => link)
            [2] => WP_Post Object ( [ID] => 34 [post_author] => 1 [post_date] => 2014-03-05 16:30:45 [post_date_gmt] => 2014-03-05 16:30:45 [post_permalink] => link)
            [3] => WP_Post Object ( [ID] => 80 [post_author] => 1 [post_date] => 2014-03-05 16:30:45 [post_date_gmt] => 2014-03-05 16:30:45 [post_permalink] => link) )

我尝试了array_merge,但这使两个子数组成为一个数组。 有没有一种方法可以达到上述效果。 任何帮助将不胜感激。 谢谢

我的数组的PHP代码:

<?php 
     $postids = array();
     $value = array();
     if ( $the_query->have_posts() ) { ?>

     while ($the_query->have_posts()) : $the_query->the_post(); 

     $postids[]=$the_query->post; 

     $value[] = get_permalink();

    ?>


    <?php endwhile; ?>

在将其放入$postids数组之前,应该应该能够将其添加到post对象中:

$postids = array();
if ($the_query->have_posts()) {
    while ($the_query->have_posts()) {
        $the_query->the_post();
        $the_query->post->post_permalink = get_permalink();
        $postids[] = $the_query->post;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM