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 have this code which gives me post_permalink in a while loop.

<?php 
  $value[] = array();
    while ($the_query->have_posts()) : $the_query->the_post()
                $value[] =array(
                    'post_permalink' => get_permalink()
                );

 endwhile; ?>

Now the thing is that I'm getting the links as

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

The way I want it to be:

        Array ( [post_permalink] => link  
                [post_permalink] => link 
                [post_permalink] => link 
                [post_permalink] => link  )

i.e: All of the links in one array instead of four subarrays. Please help!

share|improve this question
3  
The result that you say you want has the same index multiple times. I don't think that's actually what you want. –  Patrick Q Apr 15 at 13:54
    
Your array must have unique keys. There really is no way to get around this. –  sfyn Apr 15 at 13:57
    
Yes I want the same index for every value, if that's possible –  user48752 Apr 15 at 13:57
    
"if that's possible" It's not. –  Patrick Q Apr 15 at 13:58
1  
Before trying a bunch of things, you should probably spend some more time figuring out exactly what it is you're trying to accomplish, and what the result should be to accomplish that. –  Patrick Q Apr 15 at 14:03

3 Answers 3

You can't have an array the way you want it because each array key must be unique. This will work:

$values = array();
while($the_query->have_posts()) : $the_query->the_post();
    $values[] = get_permalink();
endwhile;
share|improve this answer
    
What does the colon in front of $the_query do? I've never seen this syntax before. –  max.weller Apr 15 at 16:27
    
@max.weller php.net/manual/en/control-structures.alternative-syntax.php This is the standard loop syntax in Wordpress –  Andy Apr 16 at 9:02

The example of what you want is not possible as array keys are unique.

You probably want something like:

$value['post_permalink'][] = get_permalink();
share|improve this answer
foreach($value[0] as $k=>$v)
   $result[$k]=$v
share|improve this answer

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.