利用保存在 TXT 文件中的数组数据 (PHP)
Utilize array data saved in TXT file ( PHP)
我正在尝试利用通过表单提交保存到 list.txt 的数据,并在页面上随机显示。
index.php
<?php
//$list = file_get_contents('list.txt'); //Tested without fopen
$myfile = fopen('list.txt', "r") or die ("");
$list = fread($myfile,filesize('list.txt'));
$bg0 = array($list ,'yellow'); // array of colors
fclose($myfile);
$i = rand(0, count($bg0)-1); // generate random number size of the array
$selectedColor = "$bg0[$i]"; // set variable equal to which random color was chosen
echo selectedColor;
?>
list.txt
'red', 'blue', 'green'
您需要使用 explode()
将字符串转换为数组,然后根据需要添加其他元素:
$list = fread($myfile,filesize('list.txt'));
fclose($myfile);
$bg0 = explode(', ',str_replace("'","",$list));
$bg0[] = 'yellow';
$i = rand(0, count($bg0)-1); // generate random number size of the array
$selectedColor = "$bg0[$i]"; // set variable equal to which random color was chosen
echo $selectedColor;
我不完全确定你想在这里实现什么,但你有一些问题:
1) selectedColor
应该是 $selectedColor
2) 加载 "array".
您不能简单地加载文本并期望 php 猜测格式。如果要加载文件并将其视为数组,则需要指示 php 这样做。
在您的示例中,您可以拆分文本和 trim 不需要的字符:
$list = explode(',', $list);
array_walk($list, function(&$elem) {
$elem = trim($elem, ' \'');
});
3) $selectedColor = $bg0[$i];
替换:
$selectedColor = "$bg0[$i]";
与:
$selectedColor = $bg0[$i];
4) 数组推送
此行不正确:
$bg0 = array($list ,'yellow'); // array of colors
替换为:
$bg0 = array_merge($list, ['yellow']); // array of colors
如果你想对单个数组进行操作,你可以使用 array_push
但一定要更改你稍后使用的变量。
例如:
<?php
//$list = file_get_contents('list.txt'); //Tested without fopen
$myfile = fopen('list.txt', "r") or die ("");
$list = fread($myfile,filesize('list.txt'));
$list = explode(',', $list);
array_walk($list, function(&$elem) {
$elem = trim($elem, ' \'');
});
$bg0 = array_merge($list , ['yellow']); // array of colors
fclose($myfile);
$i = rand(0, count($bg0)-1); // generate random number size of the array
$selectedColor = $bg0[$i]; // set variable equal to which random color was chosen
echo $selectedColor;
?>
- 您需要拆分输入值。这可以通过正则表达式简单地完成。
- 然后将 'yellow' 添加到列表中
- Select随机颜色
$data = "'red', 'blue', 'green'";
preg_match_all("/'(.*?)'/", $data, $matches);
$list = $matches[1];
$list[] = 'yellow';
$i = rand(0, count($list)-1);
$selectedColor = $list[$i];
echo $selectedColor;
我正在尝试利用通过表单提交保存到 list.txt 的数据,并在页面上随机显示。
index.php
<?php
//$list = file_get_contents('list.txt'); //Tested without fopen
$myfile = fopen('list.txt', "r") or die ("");
$list = fread($myfile,filesize('list.txt'));
$bg0 = array($list ,'yellow'); // array of colors
fclose($myfile);
$i = rand(0, count($bg0)-1); // generate random number size of the array
$selectedColor = "$bg0[$i]"; // set variable equal to which random color was chosen
echo selectedColor;
?>
list.txt
'red', 'blue', 'green'
您需要使用 explode()
将字符串转换为数组,然后根据需要添加其他元素:
$list = fread($myfile,filesize('list.txt'));
fclose($myfile);
$bg0 = explode(', ',str_replace("'","",$list));
$bg0[] = 'yellow';
$i = rand(0, count($bg0)-1); // generate random number size of the array
$selectedColor = "$bg0[$i]"; // set variable equal to which random color was chosen
echo $selectedColor;
我不完全确定你想在这里实现什么,但你有一些问题:
1) selectedColor
应该是 $selectedColor
2) 加载 "array".
您不能简单地加载文本并期望 php 猜测格式。如果要加载文件并将其视为数组,则需要指示 php 这样做。
在您的示例中,您可以拆分文本和 trim 不需要的字符:
$list = explode(',', $list);
array_walk($list, function(&$elem) {
$elem = trim($elem, ' \'');
});
3) $selectedColor = $bg0[$i];
替换:
$selectedColor = "$bg0[$i]";
与:
$selectedColor = $bg0[$i];
4) 数组推送
此行不正确:
$bg0 = array($list ,'yellow'); // array of colors
替换为:
$bg0 = array_merge($list, ['yellow']); // array of colors
如果你想对单个数组进行操作,你可以使用 array_push
但一定要更改你稍后使用的变量。
例如:
<?php
//$list = file_get_contents('list.txt'); //Tested without fopen
$myfile = fopen('list.txt', "r") or die ("");
$list = fread($myfile,filesize('list.txt'));
$list = explode(',', $list);
array_walk($list, function(&$elem) {
$elem = trim($elem, ' \'');
});
$bg0 = array_merge($list , ['yellow']); // array of colors
fclose($myfile);
$i = rand(0, count($bg0)-1); // generate random number size of the array
$selectedColor = $bg0[$i]; // set variable equal to which random color was chosen
echo $selectedColor;
?>
- 您需要拆分输入值。这可以通过正则表达式简单地完成。
- 然后将 'yellow' 添加到列表中
- Select随机颜色
$data = "'red', 'blue', 'green'";
preg_match_all("/'(.*?)'/", $data, $matches);
$list = $matches[1];
$list[] = 'yellow';
$i = rand(0, count($list)-1);
$selectedColor = $list[$i];
echo $selectedColor;