爆炸在PHP中有一个新的行一个数组 [英] Explode an Array that has a new line in it in PHP

查看:93
本文介绍了爆炸在PHP中有一个新的行一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个分隔阵列|。我想要做这个标识是分开的。

阵列如下: -

  myid1 |我的标题|详细说明
myid2 |我的标题|第二行说明
myid3 |我的标题|第三排的说明

我所做的,我只是用爆炸它让我想要的结果。

  $ required_cells =爆炸('|',$ bulk_array);

但问题是,(如下图所示),只有我的第一阵列正常爆炸,下一个数组的下一个第一个单元格混合由于新行。

难道我能在连续的阵列单元上阵列?

 阵列

    [0] => myid1
    [1] =>我的标题
    [2] =>详细说明
myid2
    [3] =>我的标题
    [4] =>第二行说明
myid3
    [5] =>我的标题
    [6] =>第二行说明


解决方案

爆炸的换行符,又名\\ n,然后再通过数组循环,并发生爆炸的管道,又名|

  $ bulk_array =myid1 |我的标题|详细说明
myid2 |我的标题|第二行说明
myid3 |我的标题|第三排的说明;$行=爆炸(\\ n,$ bulk_array);的foreach($线为$关键=> $线)
{
    $线[$关键] =爆炸(|,$线);
}

然后的print_r($线); 将输出:

 阵列

    [0] =>排列
        (
            [0] => myid1
            [1] =>我的标题
            [2] =>详细说明
        )    [1] =>排列
        (
            [0] => myid2
            [1] =>我的标题
            [2] =>第二行说明
        )    [2] =>排列
        (
            [0] => myid3
            [1] =>我的标题
            [2] =>第三排的说明
        )

I have an array that is separated by "|". What I wanna do is separate by this identifier.

The array is as follows:-

myid1|My Title|Detailed Description
myid2|My Title|Second Row Description
myid3|My Title|Third row description

What I did was that I just used explode on it to get my desired results.

$required_cells = explode('|', $bulk_array);

But the problem is (as shown below) that only my first array is properly exploded and the next first cell of the next array is mixed due to the "new line".

Is it possible that I can get the upper array in consecutive array cells?

Array
(
    [0] => myid1
    [1] => My Title 
    [2] => Detailed Description
myid2
    [3] => My Title 
    [4] => Second Row Description
myid3
    [5] => My Title 
    [6] => Second Row Description
)

解决方案

Explode on newlines, aka "\n" first, then loop through that array and explode on pipes, aka '|'

$bulk_array = "myid1|My Title|Detailed Description
myid2|My Title|Second Row Description
myid3|My Title|Third row description";

$lines = explode("\n", $bulk_array);

foreach ($lines as $key => $line)
{
    $lines[$key] = explode('|', $line);
}

Then print_r($lines); will output:

Array
(
    [0] => Array
        (
            [0] => myid1
            [1] => My Title
            [2] => Detailed Description
        )

    [1] => Array
        (
            [0] => myid2
            [1] => My Title
            [2] => Second Row Description
        )

    [2] => Array
        (
            [0] => myid3
            [1] => My Title
            [2] => Third row description
        )

)

这篇关于爆炸在PHP中有一个新的行一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆