如何创建一个嵌套数组并将其存储在数据库中? [英] how to create a nested array and store it in database?

查看:279
本文介绍了如何创建一个嵌套数组并将其存储在数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样

数据

 <表>
   &所述; TR>
     <第i个标题1 LT; /第i
     < TD>&PARA1 LT; / TD>
   < / TR>
 < /表>
 <表>
   &所述; TR>
     <第i个标题2'; /第i
     < TD>&PARA1 LT; / TD>
     < TD>&PARA2 LT; / TD>
     < TD>&para3各个LT; / TD>
   < / TR>
 < /表>
 <表>
   &所述; TR>
     <第i个冠军3'; /第i
     < TD>&PARA1 LT; / TD>
     < TD>&PARA2 LT; / TD>
   < / TR>
 < /表>

现在我怎么可以把这个数据并制作成一个array..it将是真正有用的,如果我能得到一个解决方案。

在我的问题我有一个表,如上图所示,并希望将数据存储在一个嵌套/多维数组。所有上述解决方案多年平均值回答我的问题。

在此先感谢


解决方案

我想我知道你在找什么?它实际上pretty简单。

您想通过&LT迭代;表> 标签第一。而随后遍历孩子 TR - TD 秒。

我包围 s的周围的 DIV 来使它们抓住容易得多。

然后我会使用jQuery因为库可以很容易地选择孩子等等等等那么对于存储到数据库中。我倒是JSON-IFY的阵列(S)

IE

\r
\r

$(文件)。就绪(函数(){\r
  。MYHTML = $('#myDiv')HTML();\r
});\r
\r
VAR tableNumber = $('#myDiv')儿童('表')长度。\r
\r
变种项= [];\r
\r
对于(i = 0; I< tableNumber;我++){\r
  VAR标题= $($(表TR TH)[I])HTML()。\r
\r
  VAR第= [];\r
\r
  $($(表TR)[I])。找到('TD')。每个(函数(){\r
    paras.push($(本)的.html());\r
  });\r
  items.push(标题,段);\r
}\r
\r
无功输出= JSON.stringify(项目);\r
\r
$('#jsonOut)HTML(输出);

\r

&LT;脚本SRC =htt​​ps://ajax.googleapis.com/ajax /libs/jquery/2.1.1/jquery.min.js\"></script>\r
\r
&LT; D​​IV ID =myDiv&GT;\r
  &LT;表&gt;\r
    &所述; TR&GT;\r
      &LT;第i个标题1 LT; /第i\r
      &LT; TD&GT;第1-1 LT; / TD&GT;\r
    &LT; / TR&GT;\r
  &LT; /表&gt;\r
  &LT;表&gt;\r
    &所述; TR&GT;\r
      &LT;第i个标题2'; /第i\r
      &LT; TD&GT;第2-1 LT; / TD&GT;\r
      &LT; TD&GT;第2-2 LT; / TD&GT;\r
      &LT; TD&GT;第2-3 LT; / TD&GT;\r
    &LT; / TR&GT;\r
  &LT; /表&gt;\r
  &LT;表&gt;\r
    &所述; TR&GT;\r
      &LT;第i个冠军3'; /第i\r
      &LT; TD&GT;第3-1&LT; / TD&GT;\r
      &LT; TD&GT;第3-2 LT; / TD&GT;\r
    &LT; / TR&GT;\r
  &LT; /表&gt;\r
&LT; / DIV&GT;\r
\r
&LT; BR&GT;\r
&LT; pre&GT;\r
 &LT; D​​IV ID =jsonOut&GT;\r
 \r
 &LT; / DIV&GT;\r
&LT; / pre&GT;

\r

\r
\r

您还可以查看 FIDDLE

希望这有助于。

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

解决方案

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.

这篇关于如何创建一个嵌套数组并将其存储在数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆