How to echo php array values

php echo array values foreach
php get array value by key
array values php
php get all values from multidimensional array
php array length
php get array value without key
php array to string
php foreach associative array

For some reason my for loop isn't working in fact its breaking the page.

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. change your script as below:

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

print_r - Manual, If given an array, values will be presented in a format that shows keys and echo htmlspecialchars(print_r($array, true)); // Works and doesn't break the output. ?>. There are so many ways of printing an array values, however the simplest method is using the foreach loop. In the following example we've iterated over the $colors array and print all its elements using the echo or print statement. Let's try it out and see how it works: $colors = array ("Red", "Green", "Blue", "Yellow", "Orange");


Replace

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

with

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

How to Print or Echo All the Values of an Array in PHP, Answer: Use the PHP foreach loop. <? php. $colors = array("Red", "Green", "Blue", "Yellow", "Orange"); // Loop through colors array. foreach($colors as $value){ echo $value . "< br>"; } ?> A comment on array_merge mentioned that array_splice is faster than array_merge for inserting values. This may be the case, but if your goal is instead to reindex a numeric array, array_values() is the function of choice.


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; ?>

PHP Arrays, An array stores multiple values in one single variable: echo "I like " . $cars[0] . ", " . $cars[1] . In PHP, the array() function is used to create an array: array();. Echo value from PHP array when brackets in key-value pairs Tag: php , arrays Quick question, how do you echo a variable when the array has key's that contain names with brackets etc.


How to echo out the values of this array?, How to echo out the values of this array? php arrays key. How to echo out the values individually of this array? Array (  I want to echo the values of all arrays that has been returned from a search function. Echo values of arrays? Ask Question Deleting an element from an array


How to echo or print an array in PHP?, 3) you can loop the array using php's foreach(); and get the desired output. information about expressions that includes its type and value. The array_values() function returns an array containing all the values of an array. Tip: The returned array will have numeric keys, starting at 0 and increase by 1.


Difference between echo and print_r in php?, Return Value: If the $variable is an integer or a float or a string the function the function prints the array in a format which displays the keys as well as values, printing the variables. print_r( $var1 );. echo "\n" ;. print_r( $var2 );. echo "\n" ;. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.