Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any php function fill an array with string values?

Tags:

arrays

php

My code:

$a = array_fill(5, 6, 'banana');

prints

Array
(
    [5]  => banana
    [6]  => banana
    [7]  => banana
    [8]  => banana
    [9]  => banana
    [10] => banana
)

But I wanted it something like below

Array
(
    [5]  => banana 1
    [6]  => banana 2
    [7]  => banana 3
    [8]  => banana 4
    [9]  => banana 5
    [10] => banana 6
)

Is there any other php function there to achieve above? If yes, could you please let me know.

like image 545
user1421214 Avatar asked Jun 29 '26 17:06

user1421214


1 Answers

function test_print_value($item2, $key, $some_value)
{
    echo $some_value." $item2<br />\n";
}

array_walk(range(0,10), 'test_print_value', 'Banana Apple ');

Output:

Banana Apple 0
Banana Apple 1
Banana Apple 2
Banana Apple 3
Banana Apple 4
Banana Apple 5
Banana Apple 6
Banana Apple 7
Banana Apple 8
Banana Apple 9
Banana Apple 10
like image 51
TigerTiger Avatar answered Jul 02 '26 06:07

TigerTiger