Php 如何洗牌字符串并包含来自第二个文件的数据?
我有2个txt文件,其中一个有问题:Php 如何洗牌字符串并包含来自第二个文件的数据?,php,shuffle,Php,Shuffle,我有2个txt文件,其中一个有问题: 1|question 1 2|question 2 ... function getTable($path_q,$path_a) { $qOpen=file($path_q); $aOpen=file($path_a); $strResult='<div id="test">'; for($i=0;$i<=sizeof($qOpen);$i++) { $masStr[$i]=expl
1|question 1
2|question 2
...
function getTable($path_q,$path_a)
{
$qOpen=file($path_q);
$aOpen=file($path_a);
$strResult='<div id="test">';
for($i=0;$i<=sizeof($qOpen);$i++)
{
$masStr[$i]=explode('"',trim($qOpen[$i]));
$masStrClear[$i] = array();
foreach($masStr[$i] as $v)
{
if (!empty($v))
{
$masStrClear[$i][]=trim($v);
}
}
for($j=0;$j<sizeof($masStrClear[$i]);$j++)
{
$question=explode('|',$masStrClear[$i][$j]);
$strResult.='<span id="q'.$question[0].'"><h4>'.$question[1].'?</h4></span>';
}
}
echo $strResult.'</div>';
}
getTable('data/test_1_q.txt','data/test_1_a.txt');
第二个答案^
1|1|answer 1 to q1
2|1|answer 2 to q1
3|2|answer 1 to q2
4|2|answer 2 to q2
...
和脚本,输出问题:
1|question 1
2|question 2
...
function getTable($path_q,$path_a)
{
$qOpen=file($path_q);
$aOpen=file($path_a);
$strResult='<div id="test">';
for($i=0;$i<=sizeof($qOpen);$i++)
{
$masStr[$i]=explode('"',trim($qOpen[$i]));
$masStrClear[$i] = array();
foreach($masStr[$i] as $v)
{
if (!empty($v))
{
$masStrClear[$i][]=trim($v);
}
}
for($j=0;$j<sizeof($masStrClear[$i]);$j++)
{
$question=explode('|',$masStrClear[$i][$j]);
$strResult.='<span id="q'.$question[0].'"><h4>'.$question[1].'?</h4></span>';
}
}
echo $strResult.'</div>';
}
getTable('data/test_1_q.txt','data/test_1_a.txt');
函数可获取($path\u q,$path\u a)
{
$qOpen=文件($path_q);
$aOpen=文件($path_a);
$strResult='';
对于($i=0;$i您的随机调用不起作用的原因是您的字段由
字符分隔,但您在上爆炸。没有”
在这些文本行中,因此explode在一个元素数组中返回一个字符串-洗牌一个元素数组是毫无意义的。我需要洗牌行,而不是第一个id。我要将;
添加到字符串的末尾,并将explode更改为$masStr[$I]=explode(';',trim($qOpen[$I]);
。但是洗牌不起作用(