0

场景是

原始来源看起来像(它没有属性 email , name....等):

a@a.com          leo
foodil@g.com     NULL

我想插入这样的记录:

首先,我有一个我需要插入的数据字段的数组

 $field = Array ( [0] => Email [1] => Name ) 

其次,我有一系列邮件

$mail = Array ( [0] => a@a.com [1] => foodil@g.com )

==================================================== ============================

最后,我有一个多维数组,其中包含数据字段,例如名称,

$set = Array ( [1] => Array ( [1] => leo [4] => NULL ) ) 

但是,它可以是多个字段,例如。它也可以有一个电话字段(还有地址,性别......无论如何),那么它将是:

它是随机索引,因为计数跳过邮件列

例如。

leo     a@a.com          4343343
NULL    foodil@g.com     3453343


   $field = Array ( [0] => Email [1] => Name  [2] => Phone ) 

   $set = Array ( [0] => Array ( [1] => leo [4] => NULL )  [2] => Array ( [1] => 4343343 [4] => 3453343 )) 

==================================================== ============================

问题是,如何在这样的场景中插入?: 查询应该是这样的

并且邮件会被测试,只有当它是真的,然后插入。例如。a@a.com 无效,那我要跳过 leo 和 4343343

$query="INSERT INTO subscriber (Email,Name,Phone) VALUES ($mail[] , $set[][], $set[][])";
4

1 回答 1

1

使用它会给出二维数组。获取数组后进行 foreach 循环并插入数据库。

$field = array ( '0' => 'Email', '1' => 'Name',  '2' =>'Phone' ) ;
$mail = array ( '0' => 'a@a.com', '1' => 'foodil@g.com' );
$set = array ( '0' => array ( '1' => 'leo', '4' => NULL ),  '2' => array ( '1' => '4343343', '4' => '3453343' )) ;

$res = array();
$key1= array_keys($mail);
foreach($key1 as $a=>$key){
    if(array_key_exists($key, $set)){
        $res[$a]['Email'] =$mail[$key]; 
        $res[$a]['Name'] = $set[$key]['1'];
        $res[$a]['Phone'] = $set[$key]['4'];
        unset($set[$key]);
    }else{
        $res[$a]['Email'] =$mail[$key];
        $res[$a]['Name'] = '';
        $res[$a]['Phone'] = '';
    }
}    
$total = count($res);
foreach($set as $q){
    $res[$total]['Email'] ='';     
    $res[$total]['Name'] = $q[1];
    $res[$total]['Phone'] = $q[4];
    $total++;
}
于 2012-05-08T09:30:21.063 回答