我有一个数组,如下所示:
echo '<table>';
foreach ($rowData as $row => $tr) {
echo '<tr>';
foreach ($tr as $td)
echo '<td>' . $td .'</td>';
echo '</tr>';
}
echo '</table>';
第二列和第四列始终是列的名称。结果类似于:
Name: John Locke - NickName: Locke
Age: 20 - Adress: Ok
看到图案了吗?
如何将这些数组放入我的数据库中?
<小时/>由于我的数据库表结构是:
ID - Name - NickName - Age - Adress
我不知道如何使用我正在使用的数组来做到这一点..
--更新
$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>';
最佳答案
你可以这样做:
$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 - 将数组转换为另一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31644534/