且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

爆炸在PHP中有一个新的行一个数组

更新时间:2023-12-04 13:44:58

爆炸的换行符,又名\\ 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
        )

)