我需要有关锻炼的帮助。它说我必须在PHP中创建一个小程序,它创建7个随机数(实际上它是一个带有6个随机数的抽奖和1个随机数(互补)在1到49之间)。这些数字必须存储在一个数组中。 问题是我必须将这些数字存储在文件中。我这样做了:
$num[1]=rand(1,49);
$num[2]=rand(1,49);
$num[3]=rand(1,49);
$num[4]=rand(1,49);
$num[5]=rand(1,49);
$num[6]=rand(1,49);
$num[7]=rand(1,49);
echo "The numbers are: "; echo $num[1]; echo "-"; echo $num[2]; echo "-"; echo $num[3]; echo "-"; echo $num[4]; echo "-"; echo $num[5]; echo "-"; echo $num[6];
echo "<br>";
echo "Complementary: ".$num[7];
然后我尝试了这样的东西来存储这些数字:
$lott=array($num[1],$num[2],$num[3],$num[4],$num[5],$num[6]);
$comp=array($num[7]);
$arch=fopen("lottery.txt","a") or die("Internal error");
fputs($arch,"Lottery numbers:");
fputs($arch,$_REQUEST['lott']);
fputs($arch,"\n");
fputs($arch,"\n");
fputs($arch, "Complementary number:");
fputs($arch,$_REQUEST['comp']);
fputs($arch,"\n");
fputs($arch,"-------------------------------------------------------");
fputs($arch,"\n");
fclose($arch);
echo "The dates have been stored correctly.";
显然它不起作用。我是php的新手,所以我不知道如何将这些数字存储在一个文件中。
我很感激帮助。
答案 0 :(得分:2)
您正在尝试将$_REQUEST['lott']
和$_REQUEST['comp']
保存到您的文件中。这些变量是否已设定?
只需保存您的$lott
和$comp
个变量即可。两者都是数组,因此必须先将其转换为字符串,然后才能保存。您可以使用implode()
。
$lott=array($num[1],$num[2],$num[3],$num[4],$num[5],$num[6]);
$comp=array($num[7]);
$arch=fopen("lottery.txt","a") or die("Internal error");
fputs($arch,"Lottery numbers:");
fputs($arch,implode(',', $lott));
fputs($arch,"\n");
fputs($arch,"\n");
fputs($arch, "Complementary number:");
fputs($arch,implode(',', $comp));
fputs($arch,"\n");
fputs($arch,"-------------------------------------------------------");
fputs($arch,"\n");
fclose($arch);
这将输出
Lottery numbers:37,27,41,1,2,14
Complementary number:19
-------------------------------------------------------
答案 1 :(得分:1)
您可以使用函数implode将数组数据连接成一个字符串:
$lott=array($num[1],$num[2],$num[3],$num[4],$num[5],$num[6]);
$comp=array($num[7]);
$arch=fopen("lottery.txt","a") or die("Internal error");
fputs($arch,"Lottery numbers:");
fputs($arch, implode(' - ', $lott); //HERE
fputs($arch,"\n");
fputs($arch,"\n");
fputs($arch, "Complementary number:");
fputs($arch,$comp[0]); //HERE you get only index 0, because your array have just one item
fputs($arch,"\n");
fputs($arch,"-------------------------------------------------------");
fputs($arch,"\n");
fclose($arch);
echo "The dates have been stored correctly.";
答案 2 :(得分:1)
以下是您的答案:代码虽然评论了---
<?php
// GENERATE 6 RANDOM NUMBERS BETWEEN 1 AND 49 WITH...
$arrRandNumbers = array();
for($i=1; $i<7; $i++){
$arrRandNumbers[] = rand(1, 49);
}
// BUILD-UP A STRING FOR THE RANDOM NUMBERS, DELIMITED WITH A NEW LINE CHARACTER
$strRandom = "";
foreach($arrRandNumbers as $randNumber){
$strRandom .= $randNumber . "\n";
}
// STORE NUMBER IN A TEXT FILE... FILE-NAME: random-numbers.txt
file_put_contents("random-numbers.txt", rtrim($strRandom, "\n"));
var_dump($arrRandNumbers);
这是另一个变体:
<?php
// GENERATE 6 RANDOM NUMBERS BETWEEN 1 AND 49 WITH...
$arrRandNumbers = array();
for($i=1; $i<7; $i++){
$arrRandNumbers[] = rand(1, 49);
}
// GENERATE A COMPLEMENTARY NUMBER: BETWEEN 1 AND 49 WITH...
$compNum = rand(1, 49);
// BUILD-UP A STRING FOR THE RANDOM NUMBERS, DELIMITED WITH 2 SPACES & WRAPPED IN SQUARE BRACKETS
$strRandom = "LOTTERY NUMBERS:" .PHP_EOL;
foreach($arrRandNumbers as $randNumber){
$strRandom .= "[" . $randNumber . "] ";
}
// TRIM-OFF THE SPACES TO THE RIGHT OF THE LOTTERY NUMBERS:
rtrim($strRandom);
// ADD THE COMPLEMENTARY NUMBER TO THE MIX WITH 3 NEW LINES IN-BETWEEN:
$strRandom .= "\n\n\nCOMPLEMENTARY NUMBER:\n" . $compNum;
// STORE NUMBER IN A TEXT FILE... FILE-NAME: random-numbers.txt
file_put_contents("random-numbers.txt", $strRandom);
var_dump($arrRandNumbers);
答案 3 :(得分:1)
{{1}}