PHP 从字符串到占位符手中的多个数组

PHP from string to multiple arrays at the hand of placeholders

美好的一天,

我有一个我觉得很奇怪的问题,我也不知道怎么问这个问题。

我想创建一个如下所示的字符串变量:

[car]Ford[/car]
[car]Dodge[/car]
[car]Chevrolet[/car]
[car]Corvette[/car]
[motorcycle]Yamaha[/motorcycle]
[motorcycle]Ducati[/motorcycle]
[motorcycle]Gilera[/motorcycle]
[motorcycle]Kawasaki[/motorcycle]

这应该被处理并且看起来像:

$variable = array(
    'car' => array(
        'Ford',
        'Dodge',
        'Chevrolet',
        'Corvette'
        ),
    'motorcycle' => array(
        'Yamaha',
        'Ducati',
        'Gilera',
        'Kawasaki'
    )
);

有人知道怎么做吗? 我想做的事情叫什么?

I want to explode the string into the two arrays. If it is a sub array or two individual arrays. I do not care. I can always combine the latter if I wish so.

But from the above mentioned string to two arrays. That is what I want.


Solution by Dlporter98

<?php
///######## GET THE STRING FILE OR DIRECT INPUT
// $str = file_get_contents('file.txt');
$str = '[car]Ford[/car]
[car]Dodge[/car]
[car]Chevrolet[/car]
[car]Corvette[/car]
[motorcycle]Yamaha[/motorcycle]
[motorcycle]Ducati[/motorcycle]
[motorcycle]Gilera[/motorcycle]
[motorcycle]Kawasaki[/motorcycle]';

$str = explode(PHP_EOL, $str);

$finalArray = [];
foreach($str as $item){
    //Use preg_match to capture the pieces of the string we want using a regular expression.
    //The first capture will grab the text of the tag itself.
    //The second capture will grab the text between the opening and closing tag.
    //The resulting captures are placed into the matches array.
    preg_match("/\[(.*?)\](.*?)\[/", $item, $matches);

    //Build the final array structure.
    $finalArray[$matches[1]][] = $matches[2];
}

print_r($finalArray);
?>

This gives me the following array:

Array
(
    [car] => Array
        (
            [0] => Ford
            [1] => Dodge
            [2] => Chevrolet
            [3] => Corvette
        )

    [motorcycle] => Array
        (
            [0] => Yamaha
            [1] => Ducati
            [2] => Gilera
            [3] => Kawasaki
        )

)

The small change I had to make was:

Change

   $finalArray[$matches[1]] = $matches[2]

To:

   $finalArray[$matches[1]][] = $matches[2];

Thanks a million!!

为什么不创建两个单独的数组?

$cars = array("Ford", "Dodge", "Chevrolet", "Corvette");

$motorcycle = array("Yamaha", "Ducati", "Gilera", "Kawasaki");

您也可以使用关联数组来执行此操作。

$variable = array("Ford"=>"car", "Yamaha"=>"motorbike");

有多种方法可以将此字符串中的信息转换为关联数组。

使用 explode 函数将新行上的字符串拆分为一个数组:

$str = "[car]Ford[/car]
[car]Dodge[/car]
[car]Chevrolet[/car]
[car]Corvette[/car]
[motorcycle]Yamaha[/motorcycle]
[motorcycle]Ducati[/motorcycle]
[motorcycle]Gilera[/motorcycle]
[motorcycle]Kawasaki[/motorcycle]";

$items = explode(PHP_EOL, $str);

此时每个分隔项现在都是一个数组条目。

Array
(
    [0] => [car]Ford[/car]
    [1] => [car]Dodge[/car]
    [2] => [car]Chevrolet[/car]
    [3] => [car]Corvette[/car]
    [4] => [motorcycle]Yamaha[/motorcycle]
    [5] => [motorcycle]Ducati[/motorcycle]
    [6] => [motorcycle]Gilera[/motorcycle]
    [7] => [motorcycle]Kawasaki[/motorcycle]
)

接下来,遍历数组并使用 preg_match function with a regular expression:

提取构建最终关联数组所需的适当部分
$finalArray = [];
foreach($items as $item)
{
    //Use preg_match to capture the pieces of the string we want using a regular expression.
    //The first capture will grab the text of the tag itself.
    //The second capture will grab the text between the opening and closing tag.
    //The resulting captures are placed into the matches array.
    preg_match("/\[(.*?)\](.*?)\[/", $item, $matches);

    //Build the final array structure.
    $finalArray[$matches[1]] = $matches[2]
}

以下是将在 foreach 循环的给定迭代的 matches 数组中找到的内容的示例。

Array
(
    [0] => [motorcycle]Gilera[
    [1] => motorcycle
    [2] => Gilera
)

请注意,我使用 PHP_EOL 常量来展开初始字符串。如果字符串是从与您 运行 此代码所在的操作系统不同的操作系统中提取的,则这可能不起作用。您可能需要将其替换为字符串正在使用的实际行尾字符。