-2

i have a data like

 <table>
   <tr>
     <th>title 1</th>
     <td>para1</td>
   </tr>
 </table>
 <table>
   <tr>
     <th>title 2</th>
     <td>para1</td>
     <td>para2</td>
     <td>para3</td>
   </tr>
 </table>
 <table>
   <tr>
     <th>title 3</th>
     <td>para1</td>
     <td>para2</td>
   </tr>
 </table>

now how can i take this data and make into an array..it will be really helpful if i can get a solution for this.

In my problem i have a table as shown above and want to store the data in a nested/ multidimensional array. All the solution above doesnot answer my question

thanks in advance

Susan Williams
  • 317
  • 3
  • 18
  • use javascript and ajax technologies to gather all the needed data and send it to server – RomanPerekhrest Feb 24 '16 at 19:37
  • Possible duplicate of [How to connect html pages to mysql database?](http://stackoverflow.com/questions/5012335/how-to-connect-html-pages-to-mysql-database) – bryce Feb 24 '16 at 19:37
  • Possible duplicate of [Displaying a nested array in an HTML table](http://stackoverflow.com/questions/30008113/displaying-a-nested-array-in-an-html-table) – Kyle Williamson Feb 24 '16 at 19:38
  • And just like that duplicate question, this kind of question is really too broad to answer on this site. – Ageonix Feb 24 '16 at 19:39
  • Possible duplicate of [What is the fastest way to convert html table to php array?](http://stackoverflow.com/questions/3553897/what-is-the-fastest-way-to-convert-html-table-to-php-array) – Ageonix Feb 24 '16 at 19:40
  • i was going to close it as a dupe of :http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php but maybe the OP will come back and add a few details –  Feb 24 '16 at 19:40
  • @KyleWilliamson i am trying to do exactly the opposite of what is been done in that post..so can you help me out with it? – Susan Williams Feb 24 '16 at 19:44

1 Answers1

1

I think I know what you are looking for... It's actually pretty simple.

You want to iterate through the <table> tags first .. And subsequently iterate the children tr - tds.

I'd surround the tables with a surrounding div to make grabbing them much easier.

Then I'd use jQuery because the library makes it easy to choose children etc etc. Then for Storage into the database .. I'd "Json-ify" the array(s)

IE

$(document).ready(function() {
  myHTML = $('#myDiv').html();
});

var tableNumber = $('#myDiv').children('table').length;

var items = [];

for (i = 0; i < tableNumber; i++) {
  var title = $($("table tr th")[i]).html();

  var paras = [];

  $($("table tr")[i]).find('td').each(function() {
    paras.push($(this).html());
  });
  items.push(title, paras);
}

var outPut = JSON.stringify(items);

$('#jsonOut').html(outPut);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="myDiv">
  <table>
    <tr>
      <th>title 1</th>
      <td>para 1-1</td>
    </tr>
  </table>
  <table>
    <tr>
      <th>title 2</th>
      <td>para 2-1</td>
      <td>para 2-2</td>
      <td>para 2-3</td>
    </tr>
  </table>
  <table>
    <tr>
      <th>title 3</th>
      <td>para 3-1</td>
      <td>para 3-2</td>
    </tr>
  </table>
</div>

<br>
<pre>
 <div id="jsonOut">
 
 </div> 
</pre>

You can also view the FIDDLE

Hope this helps.

Zak
  • 6,976
  • 2
  • 26
  • 48
  • i am trying to parse that html in php – Susan Williams Feb 25 '16 at 06:25
  • You're not being clear.. What EXACTLY are you doing? From beginning to end. Are you receiving that HTML from a database? Are you trying to do what I just did, but in PHP? Give us a 30,000 foot view of the process from beginning to end.. – Zak Feb 25 '16 at 16:40