我希望有人能在这里指出正确的方向,我在网上冲浪了几个小时,还没有找到一个解决方案。
首先,我是一个"好的"php编码员…所以这可能是显而易见的,但我迷路了。
我有一个txt文件与数组()在从拉在api事务列表。
Array
(
[orderid] => 00000000
[type] => rebill
[paystatus] => cancelled
)
Array
(
[orderid] => 000000
[type] => rebill
[paystatus] => cancelled
)
Array
(
[orderid] => 000000
[type] => refund
[paystatus] => cancelled
[productid] => 939399393
[firstName] => XXXXXX
[lastName] => XXXX
[customeremail] => XXXX
[amount] => XX.XX
[refunddate] => 2015-01-01
[refundstatus] => ok
)
有成千上万个这样的数组()…我习惯使用json等来处理每一个,但现在我完全迷失了。
我可以用
复制文件$f = fopen("merged.txt", "r");
// Read line by line until end of file
while(!feof($f)) {
echo fgets($f) . "<br />";
}
fclose($f);
…就这样了
我玩了各种各样的foreach等,不能让它工作。如果有人能指出正确的方向,我会非常感激的。
谢谢!
试试这个怎么样?
$content= file_get_contents("merged.txt");
$arrays = explode('Array',ltrim(trim($content),'Array'));
echo "<table>";
foreach($arrays as $key => $value){
$value = text_to_array("Array".$value);
echo "<tr>";
echo "<td>";
echo $value['orderid'];
echo "</td>";
echo "</tr>";
}
echo "</table>";
function text_to_array($str) {
//Initialize arrays
$keys = array();
$values = array();
$output = array();
//Is it an array?
if( substr($str, 0, 5) == 'Array' ) {
//Let's parse it (hopefully it won't clash)
$array_contents = substr($str, 7, -2);
$array_contents = str_replace(array(' ', '[', ']', '=>'), array('', '#!#', '#?#', ''), $array_contents);
$array_fields = explode("#!#", $array_contents);
//For each array-field, we need to explode on the delimiters I've set and make it look funny.
for($i = 0; $i < count($array_fields); $i++ ) {
//First run is glitched, so let's pass on that one.
if( $i != 0 ) {
$bits = explode('#?#', $array_fields[$i]);
if( $bits[0] != '' ) $output[$bits[0]] = $bits[1];
}
}
//Return the output.
return $output;
} else {
//Duh, not an array.
echo 'The given parameter is not an array.';
return null;
}
}
首先为所有数组创建一个PHP文件。不是TXT文件。
然后需要对所有数组做一次更改,即在每个变量中定义每个数组。
$var1 = Array
(
[orderid] => 00000000
[type] => rebill
[paystatus] => cancelled
)
然后直接使用value by,
echo $var1['orderid'];
谢谢