doujia7094 2015-05-30 06:14
浏览 31
已采纳

读取包含数组的文本文件 - 显示在表格中

I am hoping someone can point in the right direction here, been surfing the internet for hours and haven't found a solution.

First, I am an "OKAY" php coder .. so this might be obvi but I am lost.

I have a txt file with array() in from a pull in an api transaction list.

    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
)

There is thousands of these array()'s ... I am use to dealing with json etc to pull for each, but right now I am lost completely.

I am able to replicate the file with

$f = fopen("merged.txt", "r");

// Read line by line until end of file
while(!feof($f)) { 
    echo fgets($f) . "<br />";
}

fclose($f);

... that's it

I have played around with all kinds of foreach while etc and can't get it to work. If someone could point in the right direction, I would appreciate it so much.

Thanks!

  • 写回答

2条回答 默认 最新

  • dsh1956 2015-05-30 06:41
    关注

    how about try this?

    $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;
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入