Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
字符串到数组的转换[PHP]_Php_Arrays - Fatal编程技术网

字符串到数组的转换[PHP]

字符串到数组的转换[PHP],php,arrays,Php,Arrays,简单问题如何转换此字符串: “‘一’=>1、‘二’=>2、‘三’=>3、‘四’=>4,” 对于这样的数组: array['One'] = 1; array['Two'] = 2; array['Three'] = 3; array['Four'] = 4; 使用正则表达式和数组_组合 preg_match_all('/\'(\w+)\'\s*=>\s*(\d+)/', $str, $m); print_r(array_combine($m[1], $m[2])); 使用正则表达式和数组

简单问题如何转换此字符串:

“‘一’=>1、‘二’=>2、‘三’=>3、‘四’=>4,”

对于这样的数组:

array['One'] = 1;
array['Two'] = 2;
array['Three'] = 3;
array['Four'] = 4;

使用正则表达式和数组_组合

preg_match_all('/\'(\w+)\'\s*=>\s*(\d+)/', $str, $m);
print_r(array_combine($m[1], $m[2]));

使用正则表达式和数组\u组合

preg_match_all('/\'(\w+)\'\s*=>\s*(\d+)/', $str, $m);
print_r(array_combine($m[1], $m[2]));

您可以简单地使用php函数:

array_flip-在一个数组中交换所有键及其关联值 排列

碰撞警告:

如果某个值多次出现,则将使用最新的键作为其值 价值,所有其他的都将丢失

示例#2数组翻转()示例:碰撞

<?php
$input = array("a" => 1, "b" => 1, "c" => 2);
$flipped = array_flip($input);

print_r($flipped);
?>

您可以简单地使用php函数:

array_flip-在一个数组中交换所有键及其关联值 排列

碰撞警告:

如果某个值多次出现,则将使用最新的键作为其值 价值,所有其他的都将丢失

示例#2数组翻转()示例:碰撞

<?php
$input = array("a" => 1, "b" => 1, "c" => 2);
$flipped = array_flip($input);

print_r($flipped);
?>

这里有一个经过测试的解决方案:

    $input = "'One' => 1,'Two' => 2,'Three' => 3,'Four' => 4,";
    $gen = new ArrayGenerator($input);
    $this->assertSame([
        'One' => 1,
        'Two' => 2,
        'Three' => 3,
        'Four' => 4,
    ], $gen->translate());
这里是完整的代码

use PHPUnit\Framework\TestCase;

class FooTest extends TestCase
{
    public function testItems()
    {
        $input = "'One' => 1,'Two' => 2,'Three' => 3,'Four' => 4,";
        $parser = new Parser($input);
        $this->assertEquals([
            "'One' => 1",
            "'Two' => 2",
            "'Three' => 3",
            "'Four' => 4"
        ], $parser->items());
    }

    public function testKeyValue()
    {
        $input = "'One' => 1";
        $parser = new KeyValue($input);
        $this->assertEquals([
            "'One'",
            "1",
        ], $parser->items());
    }

    public function testKeyValueWithoutQuotas()
    {
        $input = "'One' => 1";
        $parser = new KeyValue($input);
        $this->assertEquals([
            "One",
            "1",
        ], $parser->itemsWithoutQuotas());
    }

    public function test()
    {
        $input = "'One' => 1,'Two' => 2,'Three' => 3,'Four' => 4,";
        $gen = new ArrayGenerator($input);
        $this->assertSame([
            'One' => 1,
            'Two' => 2,
            'Three' => 3,
            'Four' => 4,
        ], $gen->translate());
    }
}

class ArrayGenerator
{
    private $input;

    public function __construct(string $input)
    {
        $this->input = $input;
    }

    public function translate()
    {
        $parser = new Parser($this->input);
        $parsed = $parser->items();

        $trans = [];

        foreach ($parsed as $item) {
            $pair = new KeyValue($item);
            $trans[$pair->itemsWithoutQuotas()[0]] = (int) $pair->itemsWithoutQuotas()[1];
        }

        return $trans;
    }
}

class KeyValue
{
    private $input;

    public function __construct(string $input)
    {
        $this->input = $input;
    }

    public function items()
    {
        $exploded = explode(' => ', $this->input);
        return $exploded;
    }

    public function itemsWithoutQuotas()
    {
        $items = $this->items();
        foreach ($items as $key => $item) {
            $items[$key] = str_replace("'", "", $item);
        }
        return $items;
    }
}

class Parser
{
    private $input;

    public function __construct(string $input)
    {
        $this->input = $input;
    }

    public function items()
    {
        $exploded = explode(',', $this->input);
        $exploded = array_filter($exploded, function ($item) {
            return $item != "";
        });
        return $exploded;
    }
}

这里有一个经过测试的解决方案:

    $input = "'One' => 1,'Two' => 2,'Three' => 3,'Four' => 4,";
    $gen = new ArrayGenerator($input);
    $this->assertSame([
        'One' => 1,
        'Two' => 2,
        'Three' => 3,
        'Four' => 4,
    ], $gen->translate());
这里是完整的代码

use PHPUnit\Framework\TestCase;

class FooTest extends TestCase
{
    public function testItems()
    {
        $input = "'One' => 1,'Two' => 2,'Three' => 3,'Four' => 4,";
        $parser = new Parser($input);
        $this->assertEquals([
            "'One' => 1",
            "'Two' => 2",
            "'Three' => 3",
            "'Four' => 4"
        ], $parser->items());
    }

    public function testKeyValue()
    {
        $input = "'One' => 1";
        $parser = new KeyValue($input);
        $this->assertEquals([
            "'One'",
            "1",
        ], $parser->items());
    }

    public function testKeyValueWithoutQuotas()
    {
        $input = "'One' => 1";
        $parser = new KeyValue($input);
        $this->assertEquals([
            "One",
            "1",
        ], $parser->itemsWithoutQuotas());
    }

    public function test()
    {
        $input = "'One' => 1,'Two' => 2,'Three' => 3,'Four' => 4,";
        $gen = new ArrayGenerator($input);
        $this->assertSame([
            'One' => 1,
            'Two' => 2,
            'Three' => 3,
            'Four' => 4,
        ], $gen->translate());
    }
}

class ArrayGenerator
{
    private $input;

    public function __construct(string $input)
    {
        $this->input = $input;
    }

    public function translate()
    {
        $parser = new Parser($this->input);
        $parsed = $parser->items();

        $trans = [];

        foreach ($parsed as $item) {
            $pair = new KeyValue($item);
            $trans[$pair->itemsWithoutQuotas()[0]] = (int) $pair->itemsWithoutQuotas()[1];
        }

        return $trans;
    }
}

class KeyValue
{
    private $input;

    public function __construct(string $input)
    {
        $this->input = $input;
    }

    public function items()
    {
        $exploded = explode(' => ', $this->input);
        return $exploded;
    }

    public function itemsWithoutQuotas()
    {
        $items = $this->items();
        foreach ($items as $key => $item) {
            $items[$key] = str_replace("'", "", $item);
        }
        return $items;
    }
}

class Parser
{
    private $input;

    public function __construct(string $input)
    {
        $this->input = $input;
    }

    public function items()
    {
        $exploded = explode(',', $this->input);
        $exploded = array_filter($exploded, function ($item) {
            return $item != "";
        });
        return $exploded;
    }
}

你试过什么吗?像explode和trim??我尝试过explode,但这会放置一个像这样的数组[0]='One=>1'数组[1]='Two=>2',如果可能的话,最好是找出是什么生成了这个字符串,并对其进行更改,以便它生成一个序列化数组。你可以使用像eval这样的坏函数。i、 你试过什么了吗?像explode和trim??我尝试过explode,但这会放置一个像这样的数组[0]='One=>1'数组[1]='Two=>2',如果可能的话,最好是找出是什么生成了这个字符串,并对其进行更改,以便它生成一个序列化数组。你可以使用像eval这样的坏函数。i、 e>eval('$a=function(){return array('.'.“'One'=>1,'Two'=>2,'Three'=>3,'Four'=>4,”);};return$a();”)实际上由于结尾处的最后一个逗号,您将有一个空元素,因此需要
$array=array\u过滤器($array)
在第一个
explode()
foreach()
之间,实际上由于末尾的最后一个逗号,您将有一个空元素,因此需要
$array=array\u过滤器($array)explode()
foreach()之间的代码>