0

I have looked at similar questions but I couldn't link them to my problem. How can I convert this string into an array an print in a tabular format? Or how can I print this string in a table with Reference Number, Client Number, Contract Number, Sequence, Transaction Number, Transmission Date, Status, Response Code, Amount Collected, Next Attempt Date, Cost To Client, Amount Excluding Cost to Client as table headings and the rest data of the table.

Reference Number,Client Number,Contract Number,Sequence,Transaction Number,Transmission Date,Status,Response Code,Amount Collected,Next Attempt Date,Cost To Client ,Amout Excluding Cost To Client 2518750,2,2,1,22439195,2014-11-19 10:31:56,Successful,00,5.00,,0.00,5.00 2518753,GK000001,M8PAY004,1,22439198,2014-11-19 10:58:27,Successful,00,189.99,,0.00,189.99 2518754,GK000001,M8PAY004,1,22439199,2014-11-19 11:00:39,Successful,00,99.99,,0.00,99.99 2518755,GK000001,GK000001,1,22439200,2014-11-19 11:17:08,Successful,00,30.00,,0.00,30.00 2518756,GK000001,M8PAY004,1,22439201,2014-11-19 11:21:48,Successful,00,189.99,,0.00,189.99 2518757,GK000001,M8PAY0041,1,22439202,2014-11-19 11:39:53,Successful,00,299.99,,0.00,299.99 2518758,GK000001,M8PAY004,1,22439203,2014-11-19 12:16:13,Successful,00,299.99,,0.00,299.99 2518759,GK000001,PM8PAY004,1,22439204,2014-11-19 12:43:49,Successful,00,189.99,,0.00,189.99 2518760,GK000001,M8PAY004,1,22439205,2014-11-19 13:14:07,Failed,56,0.00,,0.00,0.00 2518761,GK000001,PM8PAY004,1,22439206,2014-11-19 13:14:31,Failed,56,0.00,,0.00,0.00 2518762,GK000001,GK000001,1,22439207,2014-11-19 13:17:07,Successful,00,30.00,,0.00,30.00 2518763,GK000001,M8PAY004,1,22439208,2014-11-19 13:20:39,Failed,56,0.00,,0.00,0.00 2518764,GK000001,M8PAY004,1,22439209,2014-11-19 13:23:16,Failed,56,0.00,,0.00,0.00 2518765,GK000001,M8PAY004,1,22439210,2014-11-19 13:24:31,Successful,00,189.99,,0.00,189.99

It is response from a webservice I am consuming. I tried to use the explode function but it gave me an array of a hundreds of element.

Thanks for the help.

5
  • You should have a look at CSV functions in PHP, I think it's what you need here. Commented Nov 20, 2014 at 9:27
  • 1
    Additionally to the link @caCtus provided, you can have a look at the answers of this question: stackoverflow.com/questions/518795/… Commented Nov 20, 2014 at 9:28
  • Wow that couldn't ve been hard. Its a simple csv string. php.net/manual/en/function.str-getcsv.php See the first 2 examples on that page Commented Nov 20, 2014 at 9:29
  • Are all your numbers really on the same line as you posted it, or are they different lines? Commented Nov 20, 2014 at 9:35
  • Yeah all the numbers are on the same line as I posted them. Commented Nov 20, 2014 at 10:58

1 Answer 1

0

You've mentioned string, so it could be easy with this:

<table>
    <?php
    $content = "Reference Number,Client Number,Contract Number,Sequence,Transaction Number,Transmission Date,Status,Response Code,Amount Collected,Next Attempt Date,Cost To Client ,Amout Excluding Cost To Client 
    2518750,2,2,1,22439195,2014-11-19 10:31:56,Successful,00,5.00,,0.00,5.00 
    2518753,GK000001,M8PAY004,1,22439198,2014-11-19 10:58:27,Successful,00,189.99,,0.00,189.99 
    2518754,GK000001,M8PAY004,1,22439199,2014-11-19 11:00:39,Successful,00,99.99,,0.00,99.99";
    $lines = explode("\n", $content);
    foreach ($lines as $line) {
        $pieces = explode(",", $line);
        echo '<tr>' . "\n";
        foreach ($pieces as $piece) {
            echo '<td>' . trim($piece) . '</td>' . "\n";
        }
        echo '</tr>' . "\n";
    }
    ?>
</table>

If you have your content in string, as caCtus sad, you can use fgetcsv function.

NOTE: As i see, the count of properties, and the count of data does not match.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This solution did the trick for me. I am still going to try the other suggestions out too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.