PHP - 数组另一个数组 [英] php - Array to Another Array
本文介绍了PHP - 数组另一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组,我显示是这样的:
回声'<表>';
的foreach($ rowData为$行=> $ TR){
呼应'< TR>';
的foreach($ TR为$ TD)
呼应'< TD>' 。 $ TD'< / TD>'。
呼应'< / TR>';
}
呼应'< /表>';
的第二和第四列中的总的列的名称。其结果是这样的:
名称:洛克 - 绰号:洛克
年龄:20 - 地址:OK
块引用>查看模式?
我怎样才能把这些数组在我的数据库?
由于我的数据库表结构是:ID - 姓名 - 绰号 - 年龄 - ADRESS
我没有线索如何做到这一点与阵列我使用..
- 更新
$表= $ HTML的>找到('表',0);
$ rowData =阵列();的foreach($表 - >找到('TR')为$行){
//初始化数组,从每一行存储单元数据
$飞行=阵列(); 的foreach($行向>找到('TD')为$细胞){ 的foreach($小区>找到('跨')为$ E){
$ E-GT&;的innerText ='';
} //单元格文本推到数组
$飞行[] = $小区>明文;
}
$ rowData [] = $飞行;
}呼应'<表>';
的foreach($ rowData为$行=> $ TR){
呼应'< TR>';
呼应'< TD>' 。 $行。 '< / TD>';
的foreach($ TR为$ TD)
呼应'< TD>' 。 $ TD'< / TD>'。
呼应'< / TR>';
}
呼应'< /表>';
解决方案你可以这样做:
$插入=;
的foreach($ rowData为$行=> $ TR){
$插入。=('。$ TR [0]。,。$ TR [0]。,。$ TR [0]。,。$ TR [0] 。'),;}
$插入= SUBSTR($插入,0,-1)
$的SQL =INSERT INTO YourTable值$插入。I have one array that i am showing like this:
echo '<table>'; foreach ($rowData as $row => $tr) { echo '<tr>'; foreach ($tr as $td) echo '<td>' . $td .'</td>'; echo '</tr>'; } echo '</table>';
The second and forth columns are always the name of the column. The result is something like:
Name: John Locke - NickName: Locke
Age: 20 - Adress: Ok
See the pattern? How can i put these array in my database? As my database table structure is:
ID - Name - NickName - Age - Adress
I don't have a clue how to do that with the array i'm using..
--UPDATE
$table = $html->find('table', 0); $rowData = array(); foreach($table->find('tr') as $row) { // initialize array to store the cell data from each row $flight = array(); foreach($row->find('td') as $cell) { foreach($cell->find('span') as $e){ $e->innertext = ''; } // push the cell's text to the array $flight[] = $cell->plaintext; } $rowData[] = $flight; } echo '<table>'; foreach ($rowData as $row => $tr) { echo '<tr>'; echo '<td>' . $row . '</td>'; foreach ($tr as $td) echo '<td>' . $td .'</td>'; echo '</tr>'; } echo '</table>';
解决方案you can do that:
$insert = ""; foreach ($rowData as $row => $tr) { $insert .= "('".$tr[0]."','".$tr[0]."','".$tr[0]."','".$tr[0]."'),"; } $insert = substr($insert, 0, -1) $sql = "Insert into YourTable values ".$insert;
这篇关于PHP - 数组另一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文