You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.5 KiB
PHP
43 lines
1.5 KiB
PHP
<?php
|
|
namespace NN\Helper;
|
|
class ArrayExtention {
|
|
public static function ToInsert($data, $table = 'test', $wht = '') {
|
|
if (count($data) > 0) {
|
|
$keys = array_keys($data[0]);
|
|
$query = 'INSERT INTO ' . $table . ' (' . implode(', ', array_map(function($u) {
|
|
return "`$u`";
|
|
}, $keys)) . ')';
|
|
|
|
$query .= "\nSELECT " . implode(', ', array_map(function($g) {
|
|
return "a.$g";
|
|
}, $keys)) . ' FROM (';
|
|
|
|
$query .= implode("\n UNION ALL \n", array_map(function($w) use ($keys) {
|
|
$selectQuery = 'SELECT ' . implode(', ', array_map(function($q) use ($w) {
|
|
if ($w[$q] !== null) {
|
|
return '"' . addslashes($w[$q]) . "\" `$q`";
|
|
} else {
|
|
return "\"-\" `$q`";
|
|
}
|
|
}, $keys));
|
|
return $selectQuery;
|
|
}, $data));
|
|
|
|
$query .= ') a';
|
|
|
|
if (is_array($wht)) {
|
|
$query .= ' LEFT JOIN ' . $table . ' ON ' . implode(' AND ', array_map(function($whtx) use($table) {
|
|
return " $table.$whtx = a.$whtx ";
|
|
}, $wht));
|
|
|
|
$query .= ' WHERE ' . implode(' AND ', array_map(function($whtx) use($table) {
|
|
return " $table.$whtx IS NULL ";
|
|
}, $wht));
|
|
}
|
|
|
|
return $query;
|
|
} else {
|
|
return [];
|
|
}
|
|
}
|
|
} |