php 将txt转换为多维数组[重复]

问题描述 投票:0回答:1

我有一个txt文件 我需要将其转换为多维数组

/tost.txt 
------------------
where-when-who
cow-dog-cat
grape-aple-grape
------------------


<?php
$a = file("tost.php");
for($i = 0;$i<count($a);$i++)
{
$b = split("\-",$a[$i]);
}
?>

我尝试了一下,但输出:

 Array ( [0] => where [1] => when [2] => who )
 Array ( [0] => cow [1] => dog [2] => cat )
 Array ( [0] => maggo [1] => aple [2] => grape )

我尝试过 array_merge() 、 array_combine() 、explode() 、 implode() ;我不需要任何人 -->

我需要输出

 echo $b[0][0];   = "where" 
 echo $b[0][1];   = "when" 

感谢您的帮助-

php arrays csv
1个回答
0
投票
<?php
$result = Array();
$a = file("tost.php");
for($i = 0;$i<count($a);$i++) {
    $b = split("-",$a[$i]);
    $result[$i] = $b;
}
?>
© www.soinside.com 2019 - 2024. All rights reserved.