爆炸然后合并数组

I have two strings, one containing names separated by commas and the other containing email addresses separated by commas.

I now want my end result to replicate the behaviour as if I had gotten those values from the database with a:

while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
    echo $row['name'].'<br />'.$row['email'];
}

So I have for example these strings:

email1@domain.com,email2@domain.com,email3@domain.com

and

name1,name2,name3

And can then explode these into value arrays. Now, after exploding them, I want to be able to make a loop where in each loop I can get $name[0] and $email[0] together, and $name[1] and $email[1] together, etc.

How can I merge the two arrays (after exploding them) and get the data for each datapair (name and email) in a loop?

So if I understood you right, you are converting your strings (lets call them email, and name) too two arrays (lets call them arrEmail, and arrName) and now you want to get a array with the merged datasets.(creating the arrays should be easy if not check this out: manual for php explode function)

If so I would create a for loop based on the length of your two arrays. In the loop I would extract value i of arrEmail and arrName, and put the information into an two-dimensional array. Maybe something like this:

It should work but I didn't test it for ages so if not leave me a comment.

<?php
  $arrEmail = array();
  $arrName = array();
 
  $arrGoal;

  $arrLength = count($arrName);
 
  //creating two-dimensional Array
  for($i = 0; $i < $arrLength; $i++){ 
    $arrGoal[$i] = array($arrName[$i], $arrEmail[$i]);
    //should look something like this ((name, email),(name, email)…)
  }
  
  $arrGoalLength = count($arrGoal);

  //accessing Array
  //1. dimension
  for($i = 0; $i < $arrGoalLength; $i++){
    //2. dimension
      //Variables should be global (but aren't)
      $newName = $arrGoal[$i][0];
      $newEmail = $arrGoal[$i][1];
  }
?>
 

</div>

I think you want something like this:

$output = array();
for ($i = 0; $i < count($names); $i++) {
    $output[] = $names[$i] . ' ' . $emails[$i];
}
print_R($output);

I think you should use the built in explode function wich will allow you to turn a string to an array by spliting it using a delimiter (comma) :

$names = explode(",",$row['name']);
$email = explode(",",$row['email']);

for merging them you should use something like this

$Array = array();
for ($j = 0; $j < count($names); $j++) {
    $Array[] = [$names[$j],$email[$j]];
}
$emails = 'email1@domain.com,email2@domain.com,email3@domain.com';
$emails = explode(',', $emails);

$names = 'name1,name2,name3';
$names = explode(',', $names);

and you can use array_combine() as follow :

$combined = array_combine($names, $emails);

foreach($combined as $name => $email){
   echo $name, ' : ', $email, '<br/>';
}

This is kind a related with your question, but only if you like to access the value by a key(e.g. access the name by email provided):

<?php

        $emails = 'email1@gmail.com,email2@gmail.com,email3@gmail.com';
        $names = 'name1,name2,name3';

        $arrayEmails = explode(',',$emails);
        $arrayNames = explode(',',$names);

        $result = array_combine( $arrayEmails, $arrayNames);



        print_r($result);

    ?>