Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 592783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:46:34+00:00 2026-05-13T15:46:34+00:00

sorry for the vague title. I’m extracting some data from a table with the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:46:35+00:00Added an answer on May 13, 2026 at 3:46 pm

    If I understand your problem correctly this is how you should do it.

    First of all I suggest you catch each row instead of individual cells and then parse each row independently.

    So in this example I assume that you row is wrapped in tr tags:

    <tr>
    <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 -->
    </tr>
    

    If there are more cells at the beginning or the end you will just have to adjust indexes accordingly. Also I havent tested this code so there might be some errors there but the general idea should be ok.

    //here we will store parsed values
    $data = array();
    
    // you may want to filter this a bit if you want some rows to be skipped
    foreach ($html->find('tr') as $tr) {
        // we get first cell in the row, find a element inside and take it's inner text and so on
        $name = $tr->children(1)->find('a')->innertext;
        $comment = $tr->children(2)->innertext;
        $monthyFee = $tr->children(3)->find('strong')->innertext;
        $pricePerMin = $tr->children(4)->innertext;
        $pricePerSms = $tr->children(5)->innertext;
        $pricePerMms = $tr->children(6)->innertext;
    
        // create new entry in $data array formatted as you wanted it
        $data[$name] = array($comment, $monthlyFee, $pricePerMin, $pricePerSms, $pricePerMms);
    }
    

    Important note here – this won’t prevent you from overwriting some data in case your name is not unique so you must be sure if it really is. This is because associative arrays cannot have multiple keys with the same value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry about the vague question title, I just want to ascertain some things. Static
Sorry for vague question title. I've got a table containing huge list of, say,
I'm sorry the question title is so vague, but I was looking at some
I'm sorry for the vague title, but what I want to do can hardly
Using MS Access. I'm sorry that the title is vague--i just don't exactly know
I'm sorry for a pretty vague title, didn't want to turn it into a
Sorry for the vague title. Here's an example of what I'd like to do.
Sorry for the vague title, as I really can't explain this problem succinctly. Basically
Sorry about a vague title, but here's what i am trying to do. I
Sorry for such a vague title. Basically, I am trying to hack a function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.