简体   繁体   English

如何回显PHP数组值

[英]How to echo php array values

For some reason my for loop isn't working in fact its breaking the page. 由于某种原因,我的for循环实际上无法正常工作,它破坏了页面。

Can you see what I've done wrong. 你能看到我做错了什么吗?

<?php 
    $args = array(
        'post_type'         => 'property',
        'posts_per_page'    => -1,
        'meta_key'          => 'property_status',
        'meta_value'        => 'For Sale'
    );

    $query = new WP_Query($args);
?>


<?php if( $query->have_posts() ): ?>
    <?php while( $query->have_posts() ): $query->the_post(); ?>
        <?php $town_array[] = get_field('town'); ?>
    <?php endwhile; ?>
    <?php 
        wp_reset_query();

        $towns = array_unique($town_array);

        for ($i = 0; $i < count($towns); $i++){
            echo "<li>"$towns[$i]"</li>"; 
        }
    ?>
<?php endif; ?>

You have to do string concatenation in echo. 您必须在echo中进行字符串连接。 change your script as below: 如下更改脚本:

 for ($i = 0; $i < count($towns); $i++){
            echo "<li>".$towns[$i]."</li>"; 
        }

Replace 更换

echo "<li>"$towns[$i]"</li>"; 

with

echo "<li>".$towns[$i]."</li>"; 

it is simpler and more readable to like this : 这样更简单易读:

<?php while( $query->have_posts() ): $query->the_post(); ?>
<li><?php the_field('town'); ?></li>
<?php endwhile; ?>

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

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