Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating 2D-array from array with foreach-loops
    primarykey
    data
    text
    <p>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.</p> <pre><code>&lt;td&gt;&lt;a href="/subname/index.jsp"&gt;SubName&lt;/a&gt;&lt;/td&gt; &lt;!-- This is the name of the subscription --&gt; &lt;td&gt;Comment regarding the subscription&lt;/td&gt;&lt;!-- Comment --&gt; &lt;td&gt;&lt;strong&gt;0,-&lt;/strong&gt;&lt;/td&gt;&lt;!-- Monthly fee --&gt; &lt;td&gt;0,49&lt;/td&gt;&lt;!-- Price per minute --&gt; &lt;td&gt;0,49&lt;/td&gt;&lt;!-- Price per SMS --&gt; &lt;td&gt;1,99&lt;/td&gt;&lt;!-- Price per MMS --&gt; </code></pre> <p>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.</p> <p>What I want is something like:</p> <blockquote> <p>Array ( [SubName1] => Array ( [0] => Comment [1] => Monthly fee [2] => Price per minute [3] => Price per SMS [4] => Price per MMS ) [SubName2] => Array ( .. )</p> </blockquote> <p>This is my code:</p> <pre><code>function getData($uri) { try { $html = file_get_html($uri); // Fetch source code $data = array(); foreach($html-&gt;find('td') as $td) { // Fetch all &lt;td&gt;-elements foreach($td-&gt;find('a') as $a) { // Fetch all &lt;a&gt;-elements to remove links $data[] = $a-&gt;innertext; // This returns the names of the subscriptions } foreach($td-&gt;find('strong') as $strong) { // Fetch all &lt;strong&gt;-elements to remove bold text $data[] = $strong-&gt;innertext; } if(!preg_match('/&lt;strong&gt;/', $td-&gt;innertext) &amp;&amp; !preg_match('/&lt;a/', $td-&gt;innertext)) { // Skip all &lt;td&gt;-elements that contains &lt;strong&gt; and &lt;a&gt;, since we already have them $data[] = $td-&gt;innertext; } } /* Logic for database insertion goes here */ unset($data); // Deletes array $html-&gt;clear(); // Clear to free up memory unset($html); } catch (Exception $e) { echo 'Failed to fetch prices from'.$uri.'.&lt;br /&gt;'.$e-&gt;getMessage(); } } </code></pre> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload