php - Creating 2D-array from array with foreach-loops -


Sorry for the unreadable title. What do I want to do to put data into 2D-array (?), Where the value of the first field is the name of a subscription and the rest is data related to that subscription.

  & lt; Td> & Lt; A href = "/ subname / index.jsp" & gt; Surname & lt; / A & gt; & Lt; / Td> & Lt ;! - This is the name of the membership - & gt; & Lt; Td> Comment about membership & lt; / Td> & Lt ;! - Comment -> & Lt; Td> & Lt; Strong & gt; 0, - & lt; / Strong> & Lt; / Td> & Lt ;! - Monthly Charges - & gt; & Lt; Td> 0,49 & lt; / Td> & Lt ;! - Cost per minute - & gt; & Lt; Td> 0,49 & lt; / Td> & Lt ;! - Cost per SMS - & gt; & Lt; Td> 1,99 & lt; / Td> & Lt ;! - Cost Per MMS - & gt;  

That is still working fine, but it puts all the values ​​in a regular array. I have tried to read on the arrays and are trying different solutions that come to mind, but I can not wrap my head around it.

What I want is something like this:

value per SMS [4] => Ara ([allName 1] => array ([0] => comment [1] => Monthly Charges [2] => Price per minute [3] => Cost per SMS [4] => Price per MMS [SubName 2] => Array (..)

This is my code:

getData ($ uri) {try {$ Html = file_get_html ($ uri); // source code $ data = array (fore) (foreach ($ html- & Gt; find ('TD') as $ td) {// Get all & lt; td> $ Td- & gt; $ ($ a) as ('A')) { // Get all data & lt; A & gt; $ Data [] = $ a- & gt; Innertext; // It returns the membership name} Get the foreign currency ($ strong- $ strong) ($ 'strong') (// bold text $ data [] = $ strong- & gt; Internal text;} to remove all & lt; Strong & gt; Receive (! Preg_match ('/ / lt; strong & gt;', $ td- & gt; internal text) & amp; amp;! Preg_match ('/ / lt; a /', $ td-> & gt; ; Internal text)) {// all & lt; Td> -Remember that the & lt; Strong & gt; And & lt; A & gt; because we already have $ data [] = $ td-> Internal text; }} / * Logic for the database entry goes here * / unset ($ data); // Simplified $ html-> Clear (); // Clear the memory to free ($ html); } Hold (exception $ E) {Fail to get values ​​from 'echo'. $ Yuri '& Lt; Br / & gt; ' $ E- & gt; Get message (); thank you in advanced.

If I understand you the problem is exactly how you should do it.

First of all, I recommend that you grab each row instead of individual cells and then parse each line independently.

In this example, I think you are wrapped in the line tr tag:

   & Lt; A href = "/ subname / index.jsp" & gt; Surname & lt; / A & gt; & Lt; / Td> & Lt ;! - This is the name of the membership - & gt; & Lt; Td> Comment about membership & lt; / Td> & Lt ;! - Comment -> & Lt; Td> & Lt; Strong & gt; 0, - & lt; / Strong> & Lt; / Td> & Lt ;! - Monthly Charges - & gt; & Lt; Td> 0,49 & lt; / Td> & Lt ;! - Cost per minute - & gt; & Lt; Td> 0,49 & lt; / Td> & Lt ;! - Cost per SMS - & gt; & Lt; Td> 1,99 & lt; / Td> & Lt ;! - Cost Per MMS - & gt; & Lt; / TR & gt;  

If there are more cells in the beginning or end then you have to adjust the index accordingly. Apart from this, I have tested this code, so there may be some errors but the general idea should be correct.

  // Here we will store the values ​​parsed with $ data = array (); // If you leave some rows in the form of foreach ($ html-> $ tr as 'tr'), then you may want to filter it a little bit {// We get the first cell in the line, Find the element inside it and it's internal text and so on $ name = $ tr-> Children (1) - & gt; Search ('a') - & gt; Innertext; $ Comment = $ tr-> Children (2) - & gt; Internal text; $ Month = $ tr- & gt; Children (3) - & gt; Find ('Strong') - & gt; Internal text; $ PricePerMin = $ tr- & gt; Children (4) - & gt; Internal text; $ PricePerSms = $ tr- & gt; Children (5) - & gt; Internal text; $ PricePerMms = $ tr- & gt; Children (6) - & gt; Internal text; / $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ / Pre> 

Important note here - this will not prevent you from overwriting some data if your name is not unique, so you have to make sure that it is really. The reason for this is that AssociateAirage can not have multiple keys with the same value.


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -