算法动态合并数组 [英] Algorithm to dynamically merge arrays

查看:161
本文介绍了算法动态合并数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创造一个PHPExcel对象每一行INSERT语句。正如我挣扎在塔内迭代(即去B1 C1 D1,得到的值,并把它们放到一个数组),我选择让每一列中所有的值,并把它们放到一个多维数组是这样的:

 阵列
(
    [FOO] =>排列
        (
            [0] => 250
            [1] => 247
            [2] => 279种
            [3] => 249
        )

    [巴] =>排列
        (
            [0] => AM PROV
            [1] => AM PROV
            [2] => AM钢笔
            [3] => AM PROV
        )

    [schoo] =>排列
        (
            [0] => XXXX
            [1] => YYY
            [2] => ZZZ
            [3] => AAA
        )
)
 

我要合并每个阵列,使得在指数0所有的数据在一个阵列,等等。我已经建立了一个通用的工具,让您从上传US preadsheet选择您需要的列。它需要首先合并列数据到单个数组,然后它应产生的INSERT语句的每个阵列。所以最终的交付是这样的:

INSERT INTO(FOO,酒吧,schoo)VALUES(250,AM PROV,XXXX);

所有帮助AP preciated。

更新:嘿,非常感谢你对你的答案。我终于设法得到它的工作使用行和单元的迭代器按​​照马克的建议,它的工作。我与它的一个单独的问题,但我认为这件事情我可以解决。再次感谢。

解决方案

 < PHP
$ uberArray =阵列(
    富=>阵列(
        0 => 250,
        1 => 247,
        2 => 279,
        3 => 249,
    ),
    酒吧=>阵列(
        0 => AM PROV
        1 => AM PROV
        2 => AM钢笔,
        3 => AM PROV
    ),
    schoo=>阵列(
        0 => XXXX,
        1 => YYY,
        2 => ZZZ,
        3 => AAA,
    )
);

$ yourMysqlLink =的mysql_connect(本地主机,用户,通行证);
的mysql_query(集名UTF8'); //根据你的编码调整

$ colNames = array_keys($ uberArray);
$ stringCols =阵列('酒吧','schoo');
$ sqlInsertStr ='INSERT INTO`your_table`(`'.implode('','',$ colNames)`VALUES \名词);

$行=阵列();
//没有真正用于遍历第一个数组,我们只需要一个循环
的foreach($ uberArray [$ colNames [0]为$ K => $ V){
    $瓦尔斯=阵列();
    的foreach($ colNames为$ V2){
        $ VAL = $ uberArray [$ V2] [$ k]的;
        如果(in_array($ 2版,$ stringCols)){
            $ VAL ='.mysql_real_escape_string($ VAL,$ yourMysqlLink)。';
        }
        $瓦尔斯[] = $ VAL;
    }
    $行[] =\ t(内爆(',',$丘壑)。);
}
$ sqlInsertStr =破灭(\ N,$行)。;;

回声'< pre风格=明确:既;>'。$ sqlInsertStr'< / pre>'; ;
 

请注意,你可能需要做一些调整,因为性能原因,如果 $ uberArray 大(如分裂字符串插入块)。或者你也可以将数据转换为CSV和使用MySQL LOAD DATA INFILE 的方法,这是真正的快。

I'm trying to create a INSERT statement for every row in a PHPExcel object. As I've struggled to iterate across the column (ie. go B1 C1 D1, get the values, and put them into an array), I've opted to get all the values for each column and put them into a multi-dimensional array that looks like this:

Array
(
    [foo] => Array
        (
            [0] => 250
            [1] => 247
            [2] => 279
            [3] => 249
        )

    [bar] => Array
        (
            [0] => AM PROV
            [1] => AM PROV
            [2] => AM PENS
            [3] => AM PROV
        )

    [schoo] => Array
        (
            [0] => xxxx
            [1] => yyy
            [2] => zzz
            [3] => aaa
        )
)

I want to merge each of the arrays so that all the data at index 0 is in one array, etc. I've built a generic tool to allow you to select the columns you want from an uploaded spreadsheet. It needs to first merge the column data into a single array and then it should generate INSERT statements for each of the arrays. So the final deliverable is this:

INSERT INTO (foo, bar, schoo) VALUES (250, "AM PROV", "xxxx");

All help appreciated.

UPDATE: Hey all, thank you very much for your answers. I finally managed to get it working using row and cell iterators as per Mark's suggestion and it's working. I have a separate issue with it now, but I think it's something I can solve. Thanks again.

解决方案

<?php
$uberArray = array(
    "foo" => array(
        0 => 250,
        1 => 247,
        2 => 279,
        3 => 249,
    ),
    "bar" => array(
        0 => "AM PROV",
        1 => "AM PROV",
        2 => "AM PENS",
        3 => "AM PROV",
    ),
    "schoo" => array(
        0 =>  "xxxx",
        1 =>  "yyy",
        2 =>  "zzz",
        3 =>  "aaa",
    )
);

$yourMysqlLink = mysql_connect('localhost', 'user', 'pass');
mysql_query('SET NAMES utf8'); // Adjust according to your encoding

$colNames = array_keys($uberArray);
$stringCols = array('bar', 'schoo');
$sqlInsertStr = 'INSERT INTO `your_table` (`'.implode('`, `', $colNames)."`) VALUES \n";

$rows = array();
// Not really for iterating the first array, we just need a loop
foreach ($uberArray[$colNames[0]] as $k => $v) {
    $vals = array();
    foreach ($colNames as $v2) {
        $val = $uberArray[$v2][$k];
        if (in_array($v2, $stringCols)) {
            $val = "'".mysql_real_escape_string($val, $yourMysqlLink)."'";
        }
        $vals[] = $val;
    }
    $rows[] = "\t(".implode(', ', $vals).")";
}
$sqlInsertStr .= implode(",\n", $rows).';';

echo '<pre style="clear:both;">'.$sqlInsertStr.'</pre>'; ;

Note that you may need to do a few adjustments for performance reasons, if $uberArray is big (e.g. splitting the insert string into chunks). Or you can convert the data to CSV and use MySQL LOAD DATA INFILE method, which is real fast.

这篇关于算法动态合并数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆