首页 > 解决方案 > 引用每个数组值

问题描述

我有一个 PHP 数组,如下所示。我需要customkey将数组的每个值都用双引号引起来。

该数组当前如下所示:

Array
(
    [test] => Array
        (
            [enrolId] => 
            [custom] => Array
                (
                    [0] => 5:5:1-5BPOMK,5:6:Cutom Product1,5:4:Reports Test
                )

            [query] => 
        )

)

输出应该是:

Array
(
    [test] => Array
        (
            [enrolId] => 
            [custom] => Array
                (
                    [0] => "5:5:1-5BPOMK","5:6:Cutom Product1","5:4:Reports Test"
                )

            [query] => 
        )

)

标签: php

解决方案


让我们用array_map()试试这样

<?php
function quoted_string($n)
{
    return '"'.$n.'"';
}

$data = '5:5:1-5BPOMK,5:6:Cutom Product1,5:4:Reports Test';
$array = explode(',',$data);
$expected = array_map("quoted_string", $array);
echo implode(',',$expected);

?>

输出:

"5:5:1-5BPOMK","5:6:Cutom Product1","5:4:Reports Test"

演示https ://3v4l.org/MdnJS


推荐阅读