Jump to content

Storing parsed data into a multidimensional array


paintedswan

Recommended Posts

Hi all :)

 

I have a question about how to store parsed data into a multidimensional arrays -- I can store items into an array, but multidimensional arrays mystify me a bit.  Any help would be greatly appreciated. 

 

I'm storing the images in a table, grouped according to their table and their descriptions, so that later when I call the images, I can display them accordingly. I'm using the Simple HTML DOM Parser.  So say I have two tables, one with three images, and another with one.  I'd like the resulting to 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
        )

)

 

This is what I have so far:

$html = str_get_html($vartables);   
$varinfo = array();
foreach($html->find('table') as $table){
        $varinfo[] = $table->innertext;
}
print_r($varinfo);

 

Which yields:

 

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.  Thanks in advance for any help. :)

Found similar problem here and was able to adapt the answer:  http://stackoverflow.com/questions/3277687/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);    
$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
            )
    
    )

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.