Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I don't if it's possible or not but I have two arrays. The array one is something like this:

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

I have another array:

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) )

Is it possible to add each of the value of first array into second array with a specific key: For example:

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) )

I tried array_merge but that made two sub arrays into an array. Is there a way I can achieve the above result. Any help would be highly appreciated. Thanks

My php code for arrays:

<?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; ?>
share|improve this question
    
Use array_push() You could use a little foreach along the way. It is doable. –  Simon_eQ 17 hours ago
    
That concept for that foreach is not coming into my mind, making me confuse :P –  user48752 17 hours ago
    
Interesting that this was asked not long after this similar question –  Patrick Q 17 hours ago
    
Lol yeah that was another silly issue with arrays I had :p –  user48752 17 hours ago
add comment

1 Answer

up vote 2 down vote accepted

You should be able to just add it to the post object before you put it into the $postids array:

$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;
    }
}
share|improve this answer
    
WOW!! hahah I'm so excited, Thanks a million for that. Such a small thing was not getting into my head –  user48752 17 hours ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.