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 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题
  • ¥20 materialstudio计算氢键脚本问题
  • ¥15 有没有代做有偿主要做数据可视化部分即可(2023全国高考更省一本线理科类)