-1

I am new in PHP. I found the usage of a simple foreach, but I want more advance. What I done until now. I am using the ''get'' form method to get some data from API integration. This data I transform them to array. Every time I have more arrays. Right now lets say I got 4967 arrays. After maybe I will have less or more.

So I am getting this

1 --> Login
2 --> Position ID
3 --> ....
4 --> ...
..
..
13 --> Margin
14 --> Login number (let's say 2005)
15 --> Position Id number (let's say 100)

So especially I want something like the below. Everytime to create one TR then 13 TH and looping it until the end

<table>
<tbody>
<tr>
<th>Login</th>
<th>Position ID</th>
..
...
<th>Margin</th>
</tr>
<tr>
<th>12435</th>
<th>132321</th>
..
...
<th>2323</th>
</tr>
<tr>
<th>342243</th>
<th>345345</th>
..
...
<th>24324</th>
</tr>
</tbody>
</table>

Please if you have the kindness somehow give me guidelines with code. I am reading for 3 days and trying but unfortunately I can't do this.

The last thing I done in my php code to create the arrays is this

$array = explode('\r\n', $encodejson);

The output is this

array (
  0 => '"login',
  1 => 'positionId',
  2 => 'openTimestamp',
  3 => 'entryPrice',
  4 => 'direction',
  5 => 'volume',
  6 => 'symbol',
  7 => 'commission',
  8 => 'swap',
  9 => 'bookType',
  10 => 'stake',
  11 => 'spreadBetting',
  12 => 'usedMargin',
  13 => '3004701',
  14 => '394254',
  15 => '2018-07-19T23:23:53.733',
  16 => '1.2495',
  17 => 'BUY',
  18 => '300000.00',
  19 => 'GBPUSD',
  20 => '1.36',
  21 => '0.00',
  22 => 'BOOK_B',
  23 => '0.00',
  24 => 'false',
  25 => '5325.30',

0 - 12 is one tr th 13 -25 is another tr th

This happening until the end (right now let's say I got 2483 arrays with this structure)

LAST UPDATE!!! I done it with this way and worked

$chunks = array_chunk($array, 13);

    echo '<table id = "customers">';
    foreach ($chunks as $chunk) {
        echo '<tr>';
        foreach ($chunk as $val) {
            printf('<td>%s</td>', $val);
        }
        echo '</tr>';
    }
    echo '</table>';

Thanks all guys for the help

2
  • please show us output of var_export($array); Commented Sep 11, 2018 at 10:53
  • Thanks for your answer. I have edit my original post. Please see it Commented Sep 11, 2018 at 11:02

1 Answer 1

2

Maybe you could tried to make chunks from the big array.

$chunks = array_chunk($array, 13);
foreach($chunks as $chunk) {
   //form the table here
   echo "<tr>";
   foreach($chuck as $value){
      echo "<th>".$value."</th>";
   }
   echo "</tr>";
}

After modifying the flat array intu chunks you will have separated child array that simulate 1 row.

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

2 Comments

I can print it like this now [ "\"login", "positionId", "openTimestamp", "entryPrice", "direction", "volume", "symbol", "commission", "swap", "bookType", "stake", "spreadBetting", "usedMargin" ], [ "3004701", "394254", "2018-07-19T23:23:53.733", "1.2495", "BUY", "300000.00", "GBPUSD", "1.36", "0.00", "BOOK_B", "0.00", "false", "5325.30" ],
Thanks a lot my friend :) It was so easy :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.