0

Problem:

I have a string that looks like this:

[1=>2,3,4][5=>6,7,8][9=>10,11,12][13=>14,15][16=>17,18]

Question:

How can you get that string into this?

1
    2
    3
    4
5
    6
    7
    8
9
    10
    11
    12
13
    14
    15
16
    17
    18
2
  • You can probably use reg expressions and groups - but as Carsten says, what did you try so far? Commented Jun 14, 2012 at 11:34
  • 1
    Maybe not useful : you seem to have tried to use a custom format to serialize an array, look at php serialize() or json_encode() Commented Jun 14, 2012 at 12:04

5 Answers 5

2

I will try with:

$input  = '[1=>2,3,4][5=>6,7,8][9=>10,11,12][13=>14,15][16=>17,18]';
$output = array();

preg_match_all('\[(\d+)=>([\d,]+)\]', $input, $matches);
foreach ($matches as $group) {
  $output[$group[1])] = explode(',', $group[2]);
}

// and print result out:

foreach ( $output as $key => $val ) {
  echo $key . '<br/>';
  foreach ( $val as $v ) {
    echo '    ' . $v . '<br/>';
  }
}
Sign up to request clarification or add additional context in comments.

Comments

2

This code:

$input  = '[1=>2,3,4][5=>6,7,8][9=>10,11,12][13=>14,15][16=>17,18]';
$regex  = '~\[(?P<keys>\d)+=>(?P<values>(?:\d+,?)+)\]~';
$result = array();

if (preg_match_all($regex, $input, $matches)) {
  foreach ($matches['keys'] as $key => $value) {
    $result[$value] = explode(',', $matches['values'][$key]);
  }
}

print_r($result);

Results to this:

Array
(
    [1] => Array
        (
            [0] => 2
            [1] => 3
            [2] => 4
        )

    [5] => Array
        (
            [0] => 6
            [1] => 7
            [2] => 8
        )

    [9] => Array
        (
            [0] => 10
            [1] => 11
            [2] => 12
        )

    [3] => Array
        (
            [0] => 14
            [1] => 15
        )

    [6] => Array
        (
            [0] => 17
            [1] => 18
        )

)

Comments

1
$str = "[1=>2,3,4][5=>6,7,8][9=>10,11,12][13=>14,15][16=>17,18]";
$str = explode("]", $str);
$finalResult = array();

foreach ($str as $element) {
    if (!empty($element)) {
        $element = substr($element, 1);
        $element = explode("=>", $element);

        // element[0] contains the key
        $element[1] = explode(",", $element[1]);
        $finalResult[$element[0]] = $element[1];
    }
}

print_r($finalResult);

Comments

1
<?php
$str = "[1=>2,3,4][5=>6,7,8][9=>10,11,12][13=>14,15][16=>17,18]";
$parts1 = explode("][",$str);

$parts1[0]=str_replace("[","",$parts1[0]);
$parts1[count($parts1)-1]=str_replace("]","",$parts1[count($parts1)-1]);

foreach($parts1 as $k=>$v){
    $parts2[]=explode("=>",$v);
}

foreach($parts2 as $k=>$v){
    echo "<div>".$v[0]."</div>";
    foreach(explode(",",$v[1]) as $key=>$value){
        echo "<div style='margin-left:20px'>".$value."</div>";
    }
}

Output Would be enter image description here

Comments

1

Using only str_replace():

$dict = array(
    '=>' => "\n\t",
    ','  => "\n\t",
    '][' => "\n",
    '['  => '',
    ']'  => '',
);

echo str_replace(array_keys($dict), array_values($dict), $str);

Will give the result you want.

1 Comment

Thank you for the solution but I made a mistake not mentioning it should be in an array.

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.