Javascript

leetcode用例导入数据库(PHP实现) import leetcode json case into database

本文主要是介绍leetcode用例导入数据库(PHP实现) import leetcode json case into database,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
<?php
// 换成你自己的json
$str = '{"headers":{"student":["name","continent"]},"rows":{"student":[["Jane","America"],["Pascal","Europe"],["Xi","Asia"],["Jack","America"]]}}';
$a = json_decode($str, true);

if (!isset($a['headers']) || !is_array($a['headers'])) {
    die("invalid json");
}

foreach ($a['headers'] as $dbname => $colnameArr) {
    $colnames = implode(',', $colnameArr);
    $sql = "insert into {$dbname} ({$colnames}) values";
    foreach ($a['rows'][$dbname] as $rowArr) {
        $rows = implode('\',\'', $rowArr);
        $sql .= "('{$rows}'),";
    }
    $sql = rtrim($sql, ',');
    $sql .= ';';
    echo $sql.PHP_EOL;
}

// 本例输出
// insert into student (name,continent) values('Jane','America'),('Pascal','Europe'),('Xi','Asia'),('Jack','America');

 

参考资料:

http://www.showsql.com/2019/08/15/import-leetcode-json-into-mysql/

 
这篇关于leetcode用例导入数据库(PHP实现) import leetcode json case into database的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!