douwang6635 2017-07-04 23:02
浏览 48
已采纳

在PHP中专门格式化数组

I have the following PHP code on my website:

<?php
    $FirstName = array("Jacob", "Noah", "Frank");
    $LastName = array("Collins", "Little", "Allen");
    $BirthDay = array(19, 31, 06);
?>

And I want it to be output like so:

("Jacob", "Collins", 19), ("Noah", "Little", 31), ("Frank", "Allen", 06)

for an SQL string, which will be:

INSERT INTO Users (FirstName, LastName, BirthDay) VALUES ("Jacob", "Collins", 19), ("Noah", "Little", 31), ("Frank", "Allen", 06);

I am quite new to PHP (only started yesterday) and was wondering how I would do this.

Thanks.

  • 写回答

4条回答 默认 最新

  • douxiezha9319 2017-07-04 23:08
    关注

    Simply, If you have same count of $FirstName, $LastName and $BirthDay,

    using for loop and array_push you can do it like this:

    $data = array();
    $count = count($FirstName);
    
    for($i = 0; $i < $count; $i++ ){
    
        $insert = [$FirstName[$i],$LastName[$i], $BirthDay[$i]];
        array_push($data,$insert);
    
    }
    

    The $data gives you formatted array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 微信小程序运行一项功能时,弹出未知错误弹框,检查代码没有问题
  • ¥15 ATAC测序生成self-pseudo replicates之前是否要进行去线粒体reads
  • ¥15 python模糊字匹配函数问题
  • ¥20 谁刷目标页面的uv记录器上数据,数据只记录跳转的数值
  • ¥30 数据库软件的安装方法
  • ¥15 一道以太网数据传输题
  • ¥15 python 下载群辉文件
  • ¥50 代码还没怎么运行但是需要代码功能调用数据
  • ¥15 vue请求不到数据,返回状态200,数据为html
  • ¥15 用白鹭引擎开发棋牌游戏的前端为什么这么难找