-1

I need to push value to an array from a for loop based on a count number found in the database.

Here is the scenario.

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

When I do print_r($numb_images), it will print the following

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

2 issues. it prints the key with braces [] which I do not want.

2nd thing is, it is printing them all in same row.

This is exactly how I need it to populate

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

So the image1 => _image1 key/pair needs to be looped from the given number.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • 1
    There is no real issue here. `[]` does not mean it is the actual content/key of it. Its just there to represent the array key. – rmondesilva Apr 19 '16 at 06:48
  • @Rizier123 thanks. I tried that. seems it helped a little but brought single quotes around key and value both. I can't use with single quotes either. thanks – Rocking Ninja Apr 19 '16 at 06:52
  • Then you probably want to use a simple foreach loop and print the key/values in your custom format. – Rizier123 Apr 19 '16 at 06:59

1 Answers1

0

Function print_r is useful if you want to print what is stored in a variable. And it's outputs variable which you are created in right way.

If you do print_r in array

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

result will be analogical:

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

So there is no issue. Function print_r prints in brackets [] key of array element.

EDIT:

You need foreach loop to show this fields. Assume that we have array like that:

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

Now to print this without knowing size of array you can use foreach:

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

Output:

image1 _image1
image2 _image2
image4 _image4
image6 _image6

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

aslawin
  • 1,981
  • 16
  • 22
  • I understand. but my wp plugin function is not working. it needs the array value exactly the way I mentioned earlier. for some reasons, when I do loop, it doesn't work. if I do manually like the example, it works – Rocking Ninja Apr 19 '16 at 06:51
  • @Ali, maybe I don't understand what the problem is. Can you explain it in clearer way? – aslawin Apr 19 '16 at 06:53
  • Sure. I will try to be more specific. There is a wordpress plugin I am making. and it has image gallery. to show X number of image uploading fields, I need to supply the array in the format I have mentioned above. I want that array to be dynamic so I am not restricted with X number of images only. But the image count can be changed using the for loop. or something like that. hope it helps understanding better. – Rocking Ninja Apr 19 '16 at 06:54
  • Thanks. there is a confusion still. The $numb_images array which you assumed is not actually available prior to run the foreach loop. What you assumed is correct and actually that is what I want to create with foreach or for loop. so it is not available, it needs to be created. I have a variable say $num=5; So the array will be created with 5 key/value pair like this $numb_images = array( 'image1' => '_image1', 'image2' => '_image2', 'image3' => '_image3', 'image4' => '_image4', 'image5' => '_image5', ); Hope it is helpful to understand better. – Rocking Ninja Apr 19 '16 at 07:16
  • Sorry, but I really don't know where is the problem...This variable `$num` - before creating array in loop is it always known? – aslawin Apr 19 '16 at 07:22
  • yes that $num value comes from a specific database table where it is stored. so when I get that value, I want to create the array with that number of key/value pairs in it. so if $num=3, I want to create the array as $numb_images = array( 'image1' => '_image1', 'image2' => '_image2', 'image3' => '_image3', ); – Rocking Ninja Apr 19 '16 at 07:25
  • Ok, so your method using `for` loop is definately correct :) It creates array exactly what you need. – aslawin Apr 19 '16 at 07:29
  • correct. but its not creating the same format as needed for the plugin to work :( – Rocking Ninja Apr 19 '16 at 07:34
  • You need to create array like this `$numb_images = array( 'image1' => '_image1', 'image2' => '_image2', 'image3' => '_image3', );` right? And it's creates that format of array. Maybe problem is in other part of code. – aslawin Apr 19 '16 at 07:38
  • yes right. I am pretty sure the other parts of the code is fine. when I do manual, it works fine. – Rocking Ninja Apr 19 '16 at 07:44
  • finally I got it working. The original for loop I posted worked. I was doing something wrong for sure. I just did a clean start with that for loop and it worked. hurrah. thank you so much for your help so far. Appreciate that. – Rocking Ninja Apr 19 '16 at 07:48