Hi, sorry for the vague title. I'm extracting some data from a table with the setup as specified below, using simple_html_dom. What I want to do is insert the data into a 2D-array (?), where the value of the first -field is the name of a subscription and the rest is data relevant to that subscription.
<td><a href="/subname/index.jsp">SubName</a></td> <!-- This is the name of the subscription -->
<td>Comment regarding the subscription</td><!-- Comment -->
<td><strong>0,-</strong></td><!-- Monthly fee -->
<td>0,49</td><!-- Price per minute -->
<td>0,49</td><!-- Price per SMS -->
<td>1,99</td><!-- Price per MMS -->
What I have so far, is working okay, but it puts all the values into a regular array. I've tried reading up on arrays and trying different solutions that've come to mind, but I just can't seem to wrap my head around it.
What I want is something like:
Array ( [SubName1] => Array ( [0] => Comment [1] => Monthly fee [2] => Price per minute [3] => Price per SMS [4] => Price per MMS ) [SubName2] => Array ( .. )
This is my code:
function getData($uri) {
try {
$html = file_get_html($uri); // Fetch source code
$data = array();
foreach($html->find('td') as $td) { // Fetch all <td>-elements
foreach($td->find('a') as $a) { // Fetch all <a>-elements to remove links
$data[] = $a->innertext; // This returns the names of the subscriptions
}
foreach($td->find('strong') as $strong) { // Fetch all <strong>-elements to remove bold text
$data[] = $strong->innertext;
}
if(!preg_match('/<strong>/', $td->innertext) && !preg_match('/<a/', $td->innertext)) { // Skip all <td>-elements that contains <strong> and <a>, since we already have them
$data[] = $td->innertext;
}
}
/* Logic for database insertion goes here */
unset($data); // Deletes array
$html->clear(); // Clear to free up memory
unset($html);
} catch (Exception $e) {
echo 'Failed to fetch prices from'.$uri.'.<br />'.$e->getMessage();
}
}
Thanks in advance.