0

The scenario is

Original source is look like (it is without the attribute email , name....etc):

[email protected]          leo
[email protected]     NULL

I would like to insert an record like this:

Firstly, i have an array of what datafield i need to insert

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

Secondly, I have an array of mail

$mail = Array ( [0] => [email protected] [1] => [email protected] )

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

Lastly, I have an multi dimension array that has datafield e.g. Name,

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

however, It can be more than one field, eg. it can be also have a field of phone (and also address, geneder...whatever) , then it will be:

It is random index because the counting skip the mail column

eg.

leo     [email protected]          4343343
NULL    [email protected]     3453343


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

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

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

The problem is , how to insert in the scenario like this? : The query should look like this

And the mail will be tested, only if it is true, then insert. eg. [email protected] is invalid, then i have to skip leo and 4343343

$query="INSERT INTO subscriber (Email,Name,Phone) VALUES ($mail[] , $set[][], $set[][])";
5
  • 1
    Not really following what $set is and why it has random indexes? Commented May 8, 2012 at 2:46
  • what's the source of the original arrays, i bet you format them to make this a lot easier. Commented May 8, 2012 at 2:47
  • updated the question, hope can slove your doubt PorridgeBear and Dagonn thank you. Commented May 8, 2012 at 2:56
  • How is [email protected] invalid.?? I mean whats your criterion for invalid and valid email?? Commented May 8, 2012 at 5:21
  • Why is there an index [4] after [1]? What does NULL mean? And someone can have multiple phone numbers, is that it? Commented May 8, 2012 at 6:55

1 Answer 1

1

Use this it will give 2D array. After getting array make foreach loop & insert into database.

$field = array ( '0' => 'Email', '1' => 'Name',  '2' =>'Phone' ) ;
$mail = array ( '0' => '[email protected]', '1' => '[email protected]' );
$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++;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.