将包含带引号的值的逗号分隔字符串转换为具有硬编码键的关联数组的数组

发布于 2024-09-15 11:38:33 字数 421 浏览 7 评论 0原文

我需要将字符串从这样转换

"name1", "b", "2", "name2", "c", "3", "name3", "b", "2", ....

为这样的数组:

$arr[0]['name'] = "name1";
$arr[0]['char'] = "b";
$arr[0]['qnt'] = "2";

$arr[1]['name'] = "name2";
$arr[1]['char'] = "c";
$arr[1]['qnt'] = "3";

$arr[2]['name'] = "name3";
$arr[2]['char'] = "b";
$arr[2]['qnt'] = "2";

我尝试使用 explode() 将字符串转换为数组,但它无法按需要工作。

I need to convert a string from this:

"name1", "b", "2", "name2", "c", "3", "name3", "b", "2", ....

to an array like this:

$arr[0]['name'] = "name1";
$arr[0]['char'] = "b";
$arr[0]['qnt'] = "2";

$arr[1]['name'] = "name2";
$arr[1]['char'] = "c";
$arr[1]['qnt'] = "3";

$arr[2]['name'] = "name3";
$arr[2]['char'] = "b";
$arr[2]['qnt'] = "2";

I tried to use explode() to convert the string to an array but it does not work as needed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

草莓酥 2024-09-22 11:38:33
$input = '"name1", "b", "2", "name2", "c", "3", "name3", "b", "2"';
$input = str_replace('"', '', $input);
$input = explode(', ', $input);

$output = array();
$i = 0;
while ($i < count($input)) {
    $output[] = array(
        'name' => $input[$i++],
        'char' => $input[$i++],
        'qnt' => $input[$i++]
    );
}

print_r($output);

输出:

Array
(
    [0] => Array
        (
            [name] => name1
            [char] => b
            [qnt] => 2
        )

    [1] => Array
        (
            [name] => name2
            [char] => c
            [qnt] => 3
        )

    [2] => Array
        (
            [name] => name3
            [char] => b
            [qnt] => 2
        )

)
$input = '"name1", "b", "2", "name2", "c", "3", "name3", "b", "2"';
$input = str_replace('"', '', $input);
$input = explode(', ', $input);

$output = array();
$i = 0;
while ($i < count($input)) {
    $output[] = array(
        'name' => $input[$i++],
        'char' => $input[$i++],
        'qnt' => $input[$i++]
    );
}

print_r($output);

Output:

Array
(
    [0] => Array
        (
            [name] => name1
            [char] => b
            [qnt] => 2
        )

    [1] => Array
        (
            [name] => name2
            [char] => c
            [qnt] => 3
        )

    [2] => Array
        (
            [name] => name3
            [char] => b
            [qnt] => 2
        )

)
挽手叙旧 2024-09-22 11:38:33

如果你不关心数组键是数字,你可以这样做:

$string = 'name1, b, 2, name2, c, 3, name3, b, 2';
print_r( array_chunk( explode(',', $string), 3 ) );

If you do not care about the array keys being numeric, you can do:

$string = 'name1, b, 2, name2, c, 3, name3, b, 2';
print_r( array_chunk( explode(',', $string), 3 ) );
转瞬即逝 2024-09-22 11:38:33

解析包含带引号的值的 csv 字符串,然后将该平面数组拆分为 3 元素块,然后使用硬编码键伪造关联行。

代码:(演示

$text = <<<TEXT
"name1", "b", "2", "name2", "c", "3", "name3", "b", "2"
TEXT;

var_export(
    array_map(
        fn($chunk) => array_combine(['name', 'char', 'qnt'], $chunk),
        array_chunk(str_getcsv($text), 3)
    )
);

Parse the csv string containing quoted values, then split that flat array into 3-element chunks, then forge associative rows using hardcoded keys.

Code: (Demo)

$text = <<<TEXT
"name1", "b", "2", "name2", "c", "3", "name3", "b", "2"
TEXT;

var_export(
    array_map(
        fn($chunk) => array_combine(['name', 'char', 'qnt'], $chunk),
        array_chunk(str_getcsv($text), 3)
    )
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文