php - 将数组转换为另一个数组

标签 php mysql arrays database

我有一个数组,如下所示:

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/

相关文章:

php - 在php中不使用while循环查看所有表字段值

mysql - Carrierwave 多文件上传 Rails 5 不工作

c - 为什么数组的地址等于它在 C 中的值?

php - 连接两个表以列出用户的所有对话

c - 如何从字符串中取出单词并将它们放入字符串数组中?在 C 中

javascript - 所有数组元素具有相同的值

php - 如何使mysql中的值按升序排列?

php - 从前端动态发送变量到 smarty 后端

php - WordPress:为什么在 init 上初始化主类后不能使用 is_user_logged_in() ?

php-redis (new Redis()) getKeys() 方法使用 "KEYS *"或 "SCAN"进行迭代?