create php array from loop and without braces in key -


i need push value array loop based on count number found in database.

here scenario.

$num_images = 5;     $numb_images = array();         ($x = 1; $x <= $num_images; $x++) {              #$numb_images[] = array('image' . $x => '_image' . $x);             $numb_images['image' . $x] = "_image" . $x;         } 

when print_r($numb_images), print following

array ( [image1] => _image1 [image2] => _image2 [image3] => _image3 [image4] => _image4 [image5] => _image5 ) 

2 issues. prints key braces [] not want.

2nd thing is, printing them in same row.

this how need populate

$numb_images = array(         'image1' => '_image1',         'image2' => '_image2',     ); 

so image1 => _image1 key/pair needs looped given number.

any highly appreciated.

thanks

function print_r useful if want print stored in variable. , it's outputs variable created in right way.

if print_r in array

$numb_images = array(         'image1' => '_image1',         'image2' => '_image2',     ); 

result analogical:

array ( [image1] => _image1 [image2] => _image2 ) 

so there no issue. function print_r prints in brackets [] key of array element.

edit:

you need foreach loop show fields. assume have array that:

$numb_images = array(         'image1' => '_image1',         'image2' => '_image2',         'image4' => '_image4',         'image6' => '_image6',     ); 

now print without knowing size of array can use foreach:

foreach($numb_images $key => $value) {     echo $key . '  ' . $value. '<br/>'; } 

output:

image1 _image1 image2 _image2 image4 _image4 image6 _image6 

more information foreach: http://php.net/manual/en/control-structures.foreach.php


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -