dsavz66262 2014-10-19 14:17
浏览 60
已采纳

如何使用PHP将数组分解为另一个数组而不是子数组?

Is there any possibility to explode values of an array to create direct a new array with these values. I'm able to do this only with a intemidiate step.

I have following array:

Array (
[0] => OtherMachines, Polymers
[1] => Engines/Pumps/Turbines, Measurement
[2] => Materials/Metallurgy
[3] => Materials/Metallurgy, ThermProcesses
)

Then I explode its values:

foreach ($array00 as $v) {
    $array01 = explode(", ", $v);
}

and receive following array:

Array
(
[0] => Array
    (
        [0] => OtherMachines
        [1] => Polymers
    )

[1] => Array
    (
        [0] => Engines/Pumps/Turbines
        [1] => Measurement
    )

[2] => Array
    (
        [0] => Materials/Metallurgy
    )

[3] => Array
    (
        [0] => Materials/Metallurgy
        [1] => ThermProcesses
    )
)

Than I have to flatten the array to get the one I wish:

// Flatten the array.
$array02 = call_user_func_array('array_merge', $array01);

Final result:

Array (
[0] => OtherMachines
[1] => Polymers
[2] => Engines/Pumps/Turbines
[3] => Measurement
[4] => Materials/Metallurgy
[5] => Materials/Metallurgy
[6] => ThermProcesses
)

Is there any way to do this direct?

Many thanks in advance.

  • 写回答

3条回答 默认 最新

  • dongrang2186 2014-10-19 14:22
    关注

    Yes, just implode it and then explode.

    $result = explode(', ', implode(', ', $array));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题