Convert perl associative array to PHP array

给你一囗甜甜゛ 提交于 2019-12-11 11:12:53

问题


Is there a php library I can use to convert an associative array in perl to an associative array in php ? If not, is there any recommended way of doing this ? Regular expressions ? A bunch of explode and implode calls ?

I want to go from

my %arrayname = (
    key1 => "Value1",
    key2 => "Value2",
    key3 => "Value3",
...

to

$arrayname = array(
        "key1" => "Value1",
        "key2" => "Value2",
        "key3" => "Value3",

回答1:


If you have Perl on your server, you use:

print 'Array(';
while (($key, $val) = each(%arrayname)) {
    print "'$key' => '$val',";
}
print ');';

You can also have a look at the PECL Perl package, this library integrates a Perl parser in PHP.

You can use an online Perl interpreter with example code and working (there is just one extra comma at the end).

I created a regex that almost works. You can try it, but it depends on the structure of the Perl array:

preg_match_all(#\%(.+)\s=|\n(.+).*#);

Example data:

my %arrayname = (
    key1 => "Value1",
    key2 => "Value2",
    key3 => "Value3"
)

You can test it with the Regular Expression Test Tool.



来源:https://stackoverflow.com/questions/4154333/convert-perl-associative-array-to-php-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!