PHP - How to combine arrays into one array in while loop?

僤鯓⒐⒋嵵緔 提交于 2019-12-24 00:54:35

问题


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!


回答1:


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

You probably want something like:

$value['post_permalink'][] = get_permalink();



回答2:


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;



回答3:


foreach($value[0] as $k=>$v)
   $result[$k]=$v


来源:https://stackoverflow.com/questions/23085749/php-how-to-combine-arrays-into-one-array-in-while-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!