duan19911992 2015-01-23 15:46
浏览 33
已采纳

爆炸然后合并数组

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?

  • 写回答

5条回答 默认 最新

  • duanfeiqu1989 2015-01-23 16:51
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么