2

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);

This 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.

2 Answers 2

2

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
        )

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

Comments

1

Try use phpQuery or another framework for parsing HTML.

3 Comments

Yes, but I wouldn't be sure how to implement them. I edited my post showing the different things I actually know how to do with the DOM element to parse the HTML, but I don't have any skills beyond that. If someone could show me how, I'd be most grateful.
If anyone can help me with how to enter the data into a multidimensional array structure, that's the part I'm having the most issues with.
Found the answer here: stackoverflow.com/questions/3277687/…, and I'll post how I adapted it once the 24 hour limit is up. :)

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.