Is the string an array?
In the following code, $STR also did not declare an array in advance to write the style of the arrays, so it is not too far-fetched, because the string can be an array, if it is OK, you can declare beforehand Ah, then the question came, this is exactly why
$str = "1234567890";
$s = ";
for ($i =0; $i <4; $i + +)
{
$k =mt_rand (1, strlen ($STR));
$s. = $str [$k-1];
}
------to solve the idea----------------------
String and array are different types, and strings can be accessed as arrays. For your example, $str [$k-1] is shorthand for substr ($STR, $k-1, 1), so you can use it.
But if this is the case, it doesn't work, so it's important to use the right type.
$str = "1234567890";
$str 1= "ABCDEFG";
$result = Array_diff ($str, $str 1); Error
Print_r ($result);
$str =array (' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 ');
$str 1=array (' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ');
$result = Array_diff ($str, $str 1); Normal
Print_r ($result);