简体   繁体   English

将许多多级数组插入mysql base

[英]Insert many multi-level arrays into mysql base

i have a function what separate my file's content. 我具有将文件内容分开的功能。 Example file: 示例文件:

001;"text1"
002;"text2"
003;"text3"
999
001;"tekst11"

Sometimes some indexes have a few values: 有时某些索引具有一些值:

120;"text2"
120;"text3"

My function: 我的功能:

function parseCSV($file) {
   $lines = file($file);
   $output = array();
   $i = -1;
   foreach($lines as $line) {
      $line = trim($line);
      if($line == "999") {
         $i++;
      } else {
         $ex = explode(";", $line, 2);
         $val = str_replace("\"", "", $ex[1]);
         //$dodaj = mysql_query("INSERT INTO `czynsz` (`$ex[$i]`) VALUES ('$val')");
         if(isset($output[$i][$ex[0]])) {         
            if(is_array($output[$i][$ex[0]])) {

               $output[$i][$ex[0]][] = $val;
            } else {
               $output[$i][$ex[0]] = array($output[$i][$ex[0]], $val);
            }
         } else {
            $output[$i][$ex[0]] = $val;
         }
         $dodaj = mysql_query("INSERT INTO `czynsz` (`$ex[$i]`) VALUES ('$val')");
      }
   }
   return $output;
}
$csvdata = parseCSV("woda.txt");


echo "<pre>\r\n";
print_r($csvdata);
echo "</pre>\r\n";

Above script makes me multi-level array tree and separate arrays from themselves by separator (999), because in my file are many arrays... When an index has many values - the script makes for him internal tree. 上面的脚本使我成为多级数组树,并用分隔符(999)将数组与它们自己分开,因为在我的文件中有许多数组...当索引具有多个值时,脚本将为他创建内部树。 For example: 例如:

Array
    (
    [0] => Array
        (
        [001] => 06-001-03-13
        [002] => 06447
        [003] => F
        ...
        [097] =>
        [120] => Array
            (
            [0] => text1
            [1] => text2
            )
        )
    [1] => Array
    ...

And there is my question: I have a problem with save this mega array in my MySQL base. 我的问题是:我将这个大型数组保存在我的MySQL库中时遇到问题。 Every index in array have the same in data base. 数组中的每个索引的数据库都相同。 When the same index will have many values (ex. 120) it can be implode. 当同一索引具有多个值(例如120)时,它可能会内爆。 Thank you with all my heart for any form of help... :( 衷心感谢您提供任何形式的帮助... :(

Create a MySQL Table: 创建一个MySQL表:

CREATE TABLE `czynsz` (
  `id` INT NOT NULL ,
  `value` VARCHAR( 255 ) NOT NULL
) ENGINE = InnoDB;

ALTER TABLE `czynsz` ADD UNIQUE `unique` ( `id` ) 

Then use the following php code to insert the data 然后使用以下php代码插入数据

ini_set('error_reporting', E_ALL); // just for debugging remove later
ini_set('display_erorrs', 1); // just for debugging remove later

function parseCSV($file) {
    $lines = file($file);
    $output = array();
    $i = 0;
    foreach($lines as $line) {
        $line = trim($line);
        if($line == "999") {
           $i++;
           continue;
        }

        list($id, $val) = explode(";", $line, 2);

        $val = str_replace("\"", "", $val);

        $result = mysql_query("
            INSERT INTO `czynsz` (
                id, 
                `value`
            ) VALUES (
                '$id',
                '$val'
            ) ON DUPLICATE KEY UPDATE 
                `value` = CONCAT(`value`, ', ', '$val')"
        );
        if(!$result) {
            die(mysql_error());
        }
        if(isset($output[$i][$id])) {
            if(is_array($output[$i][$id])) {
                $output[$i][$id][] = $val;
            } else {
                $output[$i][$id] = array($output[$i][$id], $val);
            }
        } else {
            $output[$i][$id] = $val;
        }
    }
   return $output;
}

Use the PHP sledgehammer solution... 使用PHP大锤解决方案...

Assuming that you just want to store the array and don't need to query against distinct nodes, use the serialize function to turn your array into a string representation. 假设您只想存储数组,而不必查询不同的节点,请使用serialize函数将数组转换为字符串表示形式。 You can easily pull the data and then use unserialize to get back the original array. 您可以轻松地提取数据,然后使用反unserialize来获取原始数组。

$s_arr = serialize($my_array);

$my_array = unserialize($s_arr);

If you need access to the nodes in the database, then I think that MySQL is not a good solution because of the complexity involved. 如果您需要访问数据库中的节点,那么由于涉及到复杂性,我认为MySQL并不是一个好的解决方案。 If you want a key-value store, then you should look at a NoSQL solution such as Redis . 如果需要键值存储,则应查看NoSQL解决方案,例如Redis

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM