dqg17080 2018-11-30 10:14 采纳率: 0%
浏览 107

在字符串中添加数组键的引号

I have lot of code that has arrays like $test[keyhere]. Is there any way (in text editor for example) that could automatic correct this like $test['keyhere'].

Example:

echo "This is test variable: $test[keyhere] and...";

to this:

echo "This is test variable: ".$test['keyhere']." and...";
  • 写回答

2条回答 默认 最新

  • doujia1871 2018-11-30 10:19
    关注

    You can do it with PHP array_map() :

    <?php
        function addQuotes($n)
        {
            return "'" . $n . "'";
        }
    
        $a = array(1, 2, 3, 4, 5);
        $b = array_map("addQuotes", $a);
        print_r($b);
    ?>
    

    Or when you print just add quotes:

    echo "This is test variable: '".$test['keyhere']."' and...";
    

    In Editors you can use Find And Replace (almost every text editor has this option) usually shortkey is cntrl + f for find and there is an option for replace, cntrl + shift + f is also an option for shortkey.

    评论

报告相同问题?