示例.txt数据是:
20|Charlotte Aaaa|XXXX*SALE*O9
60|Peggy Bbbbb|XXXX*SALE*O8
25|Ashley Ccccc|XXXX*SALE*O7
103|Andrew Ddddd|XXXX*SALE*O1
51|Sally EEeee|XXXX*SALE*O6
9|Richard Fffff|XXXX*SALE*O3
23|Charlotte Aaaaaaaa|XXXX*SALE*O10
150|James Ggggggg|XXXX*SALE*O1
101|Connor Hhhhhhh|XXXX*SALE*O2
90|Barbara Iiiiiiii|XXXX*SALE*O2
23|Richard Ffffff|XXXX*DROP*O4
10|Holly Jjjjjjjj|XXXX*SALE*O5
90|Barbara Iiiiiii|XXXX*SALE*10
我目前的代码是:
$handle = fopen("lock.txt", "r");
if ($handle) {
$i=1;
while (($line = fgets($handle)) !== false) {
$data = explode("|", $line);
$locks = array($i, $data);
$i++;
var_dump($locks);
}
fclose($handle);
} else {
// error opening the file.
}
目前的输出是:
array(2) { [0]=> int(1) [1]=> array(3) { [0]=> string(2) "20" [1]=> string(14) "Charlotte Aaaa" [2]=> string(14) "XXXX*SALE*O9 " } } array(2) { [0]=> int(2) [1]=> array(3) { [0]=> string(2) "60" [1]=> string(11) "Peggy Bbbbb" [2]=> string(14) "XXXX*SALE*O8 " } } array(2) { [0]=> int(3) [1]=> array(3) { [0]=> string(2) "25" [1]=> string(12) "Ashley Ccccc" [2]=> string(14) "XXXX*SALE*O7 " } } array(2) { [0]=> int(4) [1]=> array(3) { [0]=> string(3) "103" [1]=> string(12) "Andrew Ddddd" [2]=> string(14) "XXXX*SALE*O1 " } } array(2) { [0]=> int(5) [1]=> array(3) { [0]=> string(2) "51" [1]=> string(11) "Sally EEeee" [2]=> string(14) "XXXX*SALE*O6 " } } array(2) { [0]=> int(6) [1]=> array(3) { [0]=> string(1) "9" [1]=> string(13) "Richard Fffff" [2]=> string(14) "XXXX*SALE*O3 " } } array(2) { [0]=> int(7) [1]=> array(3) { [0]=> string(2) "23" [1]=> string(18) "Charlotte Aaaaaaaa" [2]=> string(15) "XXXX*SALE*O10 " } } array(2) { [0]=> int(8) [1]=> array(3) { [0]=> string(3) "150" [1]=> string(13) "James Ggggggg" [2]=> string(14) "XXXX*SALE*O1 " } } array(2) { [0]=> int(9) [1]=> array(3) { [0]=> string(3) "101" [1]=> string(14) "Connor Hhhhhhh" [2]=> string(14) "XXXX*SALE*O2 " } } array(2) { [0]=> int(10) [1]=> array(3) { [0]=> string(2) "90" [1]=> string(16) "Barbara Iiiiiiii" [2]=> string(14) "XXXX*SALE*O2 " } } array(2) { [0]=> int(11) [1]=> array(3) { [0]=> string(2) "23" [1]=> string(14) "Richard Ffffff" [2]=> string(14) "XXXX*DROP*O4 " } } array(2) { [0]=> int(12) [1]=> array(3) { [0]=> string(2) "10" [1]=> string(14) "Holly Jjjjjjjj" [2]=> string(14) "XXXX*SALE*O5 " } } array(2) { [0]=> int(13) [1]=> array(3) { [0]=> string(2) "90" [1]=> string(15) "Barbara Iiiiiii" [2]=> string(12) "XXXX*SALE*10" } }
我想做的是如下,创建一个数组,我可以在以后用于ajax搜索每一行的第三部分
因此数组格式为
$locks[0][1] = 20
$locks[0][2] = Charlotte Aaaa
$locks[0][3] = XXXX*SALE*O9
我还没有考虑整合ajax,因为我在这方面失败了。任何帮助将不胜感激。
答案 0 :(得分:1)
据我所知,你想要的输出: 基本上在循环内部,而不是将行的输出($ data)设置为数组的值。我为每一行设置了一个子数组。
$locks = array();
$handle = fopen("lock.txt", "r");
if ($handle) {
$i=0;
while (($line = fgets($handle)) !== false) {
$data = explode("|", $line);
$locks[$i] = array(
1 => $data[0],
2 => $data[1],
3 => $data[2]
);
$i++;
//var_dump($locks);
}
fclose($handle);
} else {
// error opening the file.
}
应该提供以下内容:
$locks[0][1] = 20
$locks[0][2] = Charlotte Aaaa
$locks[0][3] = XXXX*SALE*O9
$locks[1][1] = 20
$locks[1][2] = Peggy Bbbbb
$locks[1][3] = XXXX*SALE*O8
...
...
答案 1 :(得分:1)
在爆炸线后,将数组推到结果数组上:
$locks = array();
while ($line = fgets($handle)) {
$line = trim($line); // remove the newline at the end
$data = explode('|', $line);
$locks[] = $data;
}
var_dump($locks);
答案 2 :(得分:1)
如果我理解你,那么我认为这应该有效,你将获得数组arr中的数据。
<?php
$arr=array();
$handle = fopen("lock.txt", "r");
if ($handle) {
$i=1;
while (($line = fgets($handle)) !== false) {
$data = explode("|", $line);
$locks = array($i, $data);
$i++;
//var_dump($locks);
$arr[]=array(1=>$data[0],2=>$data[1],3=>$data[2]);
}
fclose($handle);
} else {
// error opening the file.
}
?>