1

Simple question how can i transform this string:

"'One' => 1,'Two' => 2,'Three' => 3,'Four' => 4,"

To an array like this :

array['One'] = 1;
array['Two'] = 2;
array['Three'] = 3;
array['Four'] = 4;
4
  • Have you tried anything? like explode and trim?? Commented Dec 6, 2018 at 10:07
  • I tryed explode but that put an array like this array[0] = 'One => 1' array[1] = 'Two => 2' Commented Dec 6, 2018 at 10:09
  • It would probably be better to work out what's generating that string and change it so that it generates a serialised array - if possible. Commented Dec 6, 2018 at 10:11
  • You can use a bad function like eval. i.e> eval('$a = function() { return array('."'One' => 1,'Two' => 2,'Three' => 3,'Four' => 4,".'); }; return $a(); ') Commented Dec 6, 2018 at 10:15

4 Answers 4

3

Use regex and array_combine

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

demo

Sign up to request clarification or add additional context in comments.

Comments

2
$string = "'One' => 1,'Two' => 2,'Three' => 3,'Four' => 4,";
$array = explode(',',$string);
foreach($array as $item){
    $new_items = explode(' => ', $item);
    $key = $new_items[0];
    $value = $new_items[1];
    $new_array[][$key] = $value;
}
var_dump($new_array);

1 Comment

Actually due to the final comma at the end you'll have an empty element so you would need $array = array_filter($array); between the first explode() and the foreach()
2

Here a tested solution:

    $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());

and here complete code

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;
    }
}

Comments

1

You can simply use the php function array_flip:

array_flip — Exchanges all keys with their associated values in an array

Warning on collision:

If a value has several occurrences, the latest key will be used as its value, and all others will be lost.

Example #2 array_flip() example : collision

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

print_r($flipped);
?>

The above example will output:

Array
(
    [1] => b
    [2] => c
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.