dongyong2063 2017-09-11 08:21
浏览 20
已采纳

将一维数组转换为二维数组

I have an array, whose structure is basically like this:

array('id,"1"', 'name,"abcd"', 'age,"30"')

I want to convert it into a two dimensional array, which has each element as key -> value:

array(array(id,1),array(name,abcd),array(age,30))

Any advice would be appreciated!

I tried this code:

foreach ($datatest as $lines => $value){
    $tok = explode(',',$value); 
    $arrayoutput[$tok[0]][$tok[1]] = $value;
}

but it didn't work.

  • 写回答

5条回答 默认 最新

  • doushuo2834 2017-09-11 08:28
    关注

    Assuming you want to remove all quotation marks as per your question:

    $oldArray = array('id,"1"', 'name,"abcd"', 'age,"30"')
    $newArray = array();
    foreach ($oldArray as $value) {
      $value = str_replace(array('"',"'"), '', $value);
      $parts = explode(',', $value);
      $newArray[] = $parts;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化