循环函数并保存到数组


Looping function and saving to array

我对 php 很陌生,但学得很快,我想做的是循环一个函数,该函数可能会生成一个随机字符串 10 次并将每个随机字符串保存到数组中。

function getRandom()
{
    $length = 5;
    $randomString = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0,     $length);
    return $randomString;
}

这是我的 get 随机字符串函数,但现在我将如何循环它设定的次数并每次$randomString保存到数组中,任何指针都很棒,

只需要声明一个数组并保存它。例如:

$arr = array(); // declare the array
for($i = 0; $i < 10; $i++) {
    $arr[] = getRandom();
}
var_dump($arr); // to check if you are getting the desired result

这应该有效:

$length = 5;
$data = array();
// Set the top value in this case I'm using 10
for ($i=0; $i <= 10; $i++) { 
    $data[$i] = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0,$length);
}
// Print to see new array
print_r($data)