poe Posted October 17, 2007 Share Posted October 17, 2007 i have a table like: <tr><td>headA</td><td>headB</td><td>headC</td><td>headD</td></tr> <tr><td>1a</td><td>1b</td><td>1c</td><td>1d</td></tr> <tr><td>2a</td><td>2b</td><td>2c</td><td>2d</td></tr> <tr><td>3a</td><td>3b</td><td>3c</td><td>3d</td></tr> <tr><td>4a</td><td>4b</td><td>4c</td><td>4d</td></tr> where there can be any number of rows and there can be any number of columns. how can i read through this and create an array for each row, and use the header row as the keys. ie have it something like: myArray[0] = array( 'headA' = '1a', 'headB' = '1b', 'headC' = '1c', 'headD' = '1c', ); myArray[1] = array( 'headA' = '2a', 'headB' = '2b', 'headC' = '2c', 'headD' = '2c', ); etc.... thanks chris Quote Link to comment https://forums.phpfreaks.com/topic/73719-solved-extract-html-table-cells-and-put-to-an-array/ Share on other sites More sharing options...
kratsg Posted October 17, 2007 Share Posted October 17, 2007 I have two problems. One, the array you want it in... is not the general format for any array really. I would better like it if it was something like this: $table = array("column1"=>array("headA","1a","2a","3a","4a"),"column2","column3","column4"); And so on. This way, you can easily reference row numbers and column numbers much more easily by going... echo $table['column1'][0]; Would output headA as that would be the 'zeroth' row in the first column, etc... My second problem is.. why? If this was produced by echoing it out in PHP, can't you just use that loop and automatically have all of this? Quote Link to comment https://forums.phpfreaks.com/topic/73719-solved-extract-html-table-cells-and-put-to-an-array/#findComment-371987 Share on other sites More sharing options...
Barand Posted October 17, 2007 Share Posted October 17, 2007 try this (php 5) <?php $str = '<table> <tr><td>headA</td><td>headB</td><td>headC</td><td>headD</td></tr> <tr><td>1a</td><td>1b</td><td>1c</td><td>1d</td></tr> <tr><td>2a</td><td>2b</td><td>2c</td><td>2d</td></tr> <tr><td>3a</td><td>3b</td><td>3c</td><td>3d</td></tr> <tr><td>4a</td><td>4b</td><td>4c</td><td>4d</td></tr> </table>'; $xml = simplexml_load_string($str); $keys = array(); foreach ($xml->tr[0] as $key) { $keys[] = (string)$key; } $k=0; foreach ($xml->tr as $row) { if ($k>0) { $data[$k-1] = array(); $k2 = 0; foreach ($row as $cell) { $data[$k-1][$keys[$k2]] = (string)$cell; $k2++; } } $k++; } echo '<pre>', print_r($data, true), '</pre>'; ?> --> Array ( [0] => Array ( [headA] => 1a [headB] => 1b [headC] => 1c [headD] => 1d ) [1] => Array ( [headA] => 2a [headB] => 2b [headC] => 2c [headD] => 2d ) [2] => Array ( [headA] => 3a [headB] => 3b [headC] => 3c [headD] => 3d ) [3] => Array ( [headA] => 4a [headB] => 4b [headC] => 4c [headD] => 4d ) ) Quote Link to comment https://forums.phpfreaks.com/topic/73719-solved-extract-html-table-cells-and-put-to-an-array/#findComment-371989 Share on other sites More sharing options...
poe Posted October 18, 2007 Author Share Posted October 18, 2007 thanks berand, But is there a way to do this in php4 - i get an error message: Call to undefined function: simplexml_load_string() try this (php 5) <?php $str = '<table> <tr><td>headA</td><td>headB</td><td>headC</td><td>headD</td></tr> <tr><td>1a</td><td>1b</td><td>1c</td><td>1d</td></tr> <tr><td>2a</td><td>2b</td><td>2c</td><td>2d</td></tr> <tr><td>3a</td><td>3b</td><td>3c</td><td>3d</td></tr> <tr><td>4a</td><td>4b</td><td>4c</td><td>4d</td></tr> </table>'; $xml = simplexml_load_string($str); $keys = array(); foreach ($xml->tr[0] as $key) { $keys[] = (string)$key; } $k=0; foreach ($xml->tr as $row) { if ($k>0) { $data[$k-1] = array(); $k2 = 0; foreach ($row as $cell) { $data[$k-1][$keys[$k2]] = (string)$cell; $k2++; } } $k++; } echo '<pre>', print_r($data, true), '</pre>'; ?> --> Array ( [0] => Array ( [headA] => 1a [headB] => 1b [headC] => 1c [headD] => 1d ) [1] => Array ( [headA] => 2a [headB] => 2b [headC] => 2c [headD] => 2d ) [2] => Array ( [headA] => 3a [headB] => 3b [headC] => 3c [headD] => 3d ) [3] => Array ( [headA] => 4a [headB] => 4b [headC] => 4c [headD] => 4d ) ) Quote Link to comment https://forums.phpfreaks.com/topic/73719-solved-extract-html-table-cells-and-put-to-an-array/#findComment-372413 Share on other sites More sharing options...
Barand Posted October 18, 2007 Share Posted October 18, 2007 Before I upgraded to 5.0, I used to use a simplexml class that I found on phpclasses.com. It workd in a very similar way. Quote Link to comment https://forums.phpfreaks.com/topic/73719-solved-extract-html-table-cells-and-put-to-an-array/#findComment-372494 Share on other sites More sharing options...
Barand Posted October 18, 2007 Share Posted October 18, 2007 Found a copy - attached [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/73719-solved-extract-html-table-cells-and-put-to-an-array/#findComment-372497 Share on other sites More sharing options...
poe Posted October 18, 2007 Author Share Posted October 18, 2007 thanks... i must be an idiot 'cuz i still cant get it to go.. i did find something else and am able to get a result that looks like Array ( [0] => Array ( [0] => headA [1] => headB [2] => headC [3] => headD ) [1] => Array ( [0] => 1a [1] => 1b [2] => 1c [3] => 1d ) [2] => Array ( [0] => 2a [1] => 2b [2] => 2c [3] => 2d ) [3] => Array ( [0] => 3a [1] => 3b [2] => 3c [3] => 3d ) [4] => Array ( [0] => 4a [1] => 4b [2] => 4c [3] => 4d ) ) i just cant figure how to get the keys to be named 'headA', headB etc... Quote Link to comment https://forums.phpfreaks.com/topic/73719-solved-extract-html-table-cells-and-put-to-an-array/#findComment-372728 Share on other sites More sharing options...
Barand Posted October 18, 2007 Share Posted October 18, 2007 My code shows you how to do that. Put cells of first row in array $keys. Giving : $keys = array ( 0 => "HeadA", 1 => "HeadB", 2 => "HeadC", 3 => "HeadD" ); When you get the other cells, instead of placing in $data[0], place in $data[$keys[0]] etc Quote Link to comment https://forums.phpfreaks.com/topic/73719-solved-extract-html-table-cells-and-put-to-an-array/#findComment-372740 Share on other sites More sharing options...
poe Posted October 18, 2007 Author Share Posted October 18, 2007 woohoo! i got it. thankyou barand! Quote Link to comment https://forums.phpfreaks.com/topic/73719-solved-extract-html-table-cells-and-put-to-an-array/#findComment-372776 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.