Note that there are some explanatory texts on larger screens.

plurals
  1. PORemove rowspan on tables in PHP
    primarykey
    data
    text
    <p>There is this table i want to load into a multi-dimensional array. The problem is that since the table has rowspan values each line may have different cell counts. So i have to remove rowspan and add null values instead of these cells.</p> <p>This is the table i have(Original file)(Have like 5k rows.)<img src="https://i.stack.imgur.com/d5foQ.jpg" alt="Original Table"></p> <p>I have to add this table like this in order to have a proper array.<img src="https://i.stack.imgur.com/Z2biz.jpg" alt="enter image description here"></p> <p>Removing colspan values for the first line was easy. But removing rowspans in current method sometimes cause extra values in array.</p> <p>My current PHP file for this:</p> <pre><code>&lt;?php ini_set('display_errors', true); ini_set('mbstring.internal_encoding','UTF-8'); ini_set("memory_limit", "1024M"); ini_set('max_execution_time', 300); include('simple_html_dom.php'); // Create a DOM object $html = new simple_html_dom(); $html-&gt;load_file('stok.html'); $table = array(); $kac = array(); foreach($html-&gt;find('tr') as $row) { $satir = array(); $j = 0; foreach($row-&gt;find('td') as $element) { if($kac[$j]['deger']&gt;0){ $satir[]=''; $kac[$j]['deger']=$kac[$j]['deger']-1; $j++; while($kac[$j]['deger']&gt;0){ $satir[]=''; $kac[$j]['deger']=$kac[$j]['deger']-1; $j++; } }else{ $j++; if(isset($element-&gt;rowspan)){ $kac[$j]['deger']=($element-&gt;rowspan)-1; } $satir[] = str_replace('&amp;nbsp;', '', strip_tags($element-&gt;innertext)); } if(isset($element-&gt;colspan)){ $sayi=($element-&gt;colspan)-1; for($i=1;$i&lt;=$sayi;$i++){ $satir[] = ''; } } } $table[] = $satir; } echo '&lt;pre&gt;'; print_r($table); echo '&lt;/pre&gt;'; ?&gt; </code></pre> <p>My Current Output Sample: (See some Array values has 21, 23 and 17 items in it. Correct one is 21 items. (20 as index value)) --Didn't remove the table values in example output--</p> <pre><code>Array ( [0] =&gt; Array ( ) [1] =&gt; Array ( [0] =&gt; Envanter (R/B/K) (Filitre Kodu : sa) (Envanter Tarihi :28/11/2012 ) (Depo : 100) [1] =&gt; [2] =&gt; [3] =&gt; [4] =&gt; [5] =&gt; [6] =&gt; [7] =&gt; [8] =&gt; [9] =&gt; [10] =&gt; [11] =&gt; [12] =&gt; [13] =&gt; [14] =&gt; [15] =&gt; [16] =&gt; [17] =&gt; [18] =&gt; [19] =&gt; [20] =&gt; ) [2] =&gt; Array ( [0] =&gt; Model [1] =&gt; Stok Adı [2] =&gt; R [3] =&gt; Renk Adı [4] =&gt; B [5] =&gt; B [6] =&gt; B [7] =&gt; B [8] =&gt; B [9] =&gt; B [10] =&gt; B [11] =&gt; B [12] =&gt; B [13] =&gt; B [14] =&gt; B [15] =&gt; B [16] =&gt; B [17] =&gt; B [18] =&gt; B [19] =&gt; Toplam [20] =&gt; Resim ) [3] =&gt; Array ( [0] =&gt; [1] =&gt; [2] =&gt; [3] =&gt; [4] =&gt; 34 [5] =&gt; 36 [6] =&gt; 38 [7] =&gt; 40 [8] =&gt; 42 [9] =&gt; 44 [10] =&gt; 46 [11] =&gt; 48 [12] =&gt; 50 [13] =&gt; 52 [14] =&gt; 54 [15] =&gt; 56 [16] =&gt; 58 [17] =&gt; 60 [18] =&gt; 62 [19] =&gt; Toplam [20] =&gt; ) [4] =&gt; Array ( [0] =&gt; 1K011621110 [1] =&gt; NIHAN 2111 KABAN [2] =&gt; 064 [3] =&gt; FES [4] =&gt; [5] =&gt; [6] =&gt; [7] =&gt; [8] =&gt; [9] =&gt; [10] =&gt; [11] =&gt; [12] =&gt; 1.00 [13] =&gt; [14] =&gt; [15] =&gt; [16] =&gt; [17] =&gt; [18] =&gt; [19] =&gt; 1.00 [20] =&gt; Resim ) [5] =&gt; Array ( [0] =&gt; [1] =&gt; [2] =&gt; Toplam : [3] =&gt; [4] =&gt; [5] =&gt; [6] =&gt; [7] =&gt; [8] =&gt; [9] =&gt; [10] =&gt; [11] =&gt; [12] =&gt; [13] =&gt; [14] =&gt; [15] =&gt; 1.00 [16] =&gt; [17] =&gt; [18] =&gt; [19] =&gt; [20] =&gt; [21] =&gt; [22] =&gt; 1.00 [23] =&gt; ) [6] =&gt; Array ( [0] =&gt; [1] =&gt; 34 [2] =&gt; 36 [3] =&gt; 38 [4] =&gt; 40 [5] =&gt; 42 [6] =&gt; 44 [7] =&gt; 46 [8] =&gt; 48 [9] =&gt; 50 [10] =&gt; 52 [11] =&gt; 54 [12] =&gt; 56 [13] =&gt; 58 [14] =&gt; 60 [15] =&gt; 62 [16] =&gt; Toplam [17] =&gt; ) [7] =&gt; Array ( [0] =&gt; 1K011624760 [1] =&gt; NIHAN 2476 KABAN [2] =&gt; 001 [3] =&gt; SIYAH [4] =&gt; [5] =&gt; [6] =&gt; [7] =&gt; [8] =&gt; [9] =&gt; 1.00 [10] =&gt; [11] =&gt; 1.00 [12] =&gt; [13] =&gt; [14] =&gt; [15] =&gt; [16] =&gt; [17] =&gt; [18] =&gt; [19] =&gt; 2.00 [20] =&gt; Resim ) </code></pre> <p>Thanks in advance.</p> <p><strong>UPDATE FOR SOLUTION WITH WORKING CODE:</strong> Currently fills all empty cells with "***"</p> <pre><code>&lt;?php ini_set('display_errors', true); ini_set('mbstring.internal_encoding','UTF-8'); ini_set("memory_limit", "1024M"); ini_set('max_execution_time', 300); include('simple_html_dom.php'); // Create a DOM object $html = new simple_html_dom(); $html-&gt;load_file('stok.html'); $satir = array(); $rowcount = 0; foreach($html-&gt;find('tr') as $row) { $colcount = 0; foreach($row-&gt;find('td') as $element) { while($satir[$rowcount][$colcount]!=''){ $colcount++; } $satir[$rowcount][$colcount] = strip_tags(str_replace('&amp;nbsp;', '***', $element-&gt;innertext)); if(isset($element-&gt;colspan)){ $sayi=($element-&gt;colspan)-1; for($i=1;$i&lt;=$sayi;$i++){ $satir[$rowcount][$colcount+$i] = '***'; } } if(isset($element-&gt;rowspan)){ $sayi=($element-&gt;rowspan)-1; for($i=1;$i&lt;=$sayi;$i++){ $satir[$rowcount+$i][$colcount] = '***'; } } $colcount++; } $rowcount++; } echo '&lt;pre&gt;'; print_r($satir); echo '&lt;/pre&gt;'; ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload