Parse HTML table into multidimensional array
I'd like to store images (which are in tables) in a multidimensional array, grouped according to their table and their descriptions, so that later when I call the images, I can display them accordingly. For example, say I have two tables, one with three images, and another with one.
I'd like the resulting array to look like:
Array
(
[0] => Array
(
[0] => image1.jpg
[1] => 1st Variation Description
)
[1] => Array
(
[0] => image2.jpg
[1] => image3.jpg
[2] => image4.jpg
[3] => 2nd Variation Description
)
)
Edit Thanks for the recommendations on the Simple HTML Dom Parser. Here's what I've done so far, and I've reached a bit of a plateau as to how to store the data in the exact structure I need.
$html = str_get_html($vartables);
$varinfo = array();
foreach($html->find('table') as $table){
$varinfo[] = $table->innertext;
}
print_r($varinfo);
T开发者_运维问答his gives me something to the effect of:
Array
(
[0] =>
<tr>
<td width=150>
Description1
</td>
<td><a href="image1.jpg">
<img src="image1" height=100 border=1></a>
</td>
</tr>
[1] =>
<tr>
<td width=150>
Description2
</td>
<td><a href="image2.jpg">
<img src="image2.jpg" height=200 border=1></a>
</td>
<td><a href="image3.jpg">
<img src="image3.jpg" height=200 border=1></a>
</td>
<td><a href="image4.jpg">
<img src="image4.jpg" height=200 border=1></a>
</td>
</tr>
)
I'd like to strip out the html and keep the .jpg's and descriptions together in a multidimensional array...unfortunately my newbness is getting the better of me there, I'm researching but running into a roadblock.
Found similar problem here and was able to adapt the answer: how to print cells of a table with simple html dom
The only difference from my original structure being that the description is the first value in an array instead of the last, which is better I think.
$html = str_get_html($vartables);
$html = str_get_html($vartables);
$theData = array();
foreach($html->find('table') as $onetable){
foreach($onetable->find('tr') as $row) {
$rowData = array();
foreach($row->find('td') as $cell) {
if(substr_count($cell->innertext,"src")>0){
foreach($cell->find('img') as $element) {
$rowData[] = $element->src;
}
}else{
$rowData[] = $cell->innertext;
}
}
$theData[] = $rowData;
}
}
print_r($theData);
Outputs:
Array
(
[0] => Array
(
[0] => Description1
[1] => image1.jpg
)
[1] => Array
(
[0] => Description2
[1] => image2.jpg
[2] => image3.jpg
[3] => image4.jpg
)
)
Try use phpQuery or another framework for parsing HTML.
精彩评论