phparraysstring-parsing

Convert a flat array of strings resembling array-syntax into a 2d array


I can't figure out how to effectively convert the values of this array from a string to array. I really appreciate any suggestion.

    array(6) {
  ["A"]=>
  string(31) "['B' => 3, 'C' => 5, 'D' => 9],"
  ["B"]=>
  string(41) "['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7],"
  ["C"]=>
  string(51) "['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3],"
  ["D"]=>
  string(51) "['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2],"
  ["E"]=>
  string(41) "['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5],"
  ["F"]=>
  string(31) "['C' => 3, 'D' => 2, 'E' => 5],"
}

Desired output:

    $graph = [
    'A' => ['B' => 3, 'C' => 5, 'D' => 9],
    'B' => ['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7],
    'C' => ['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3],
    'D' => ['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2],
    'E' => ['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5],
    'F' => ['C' => 3, 'D' => 2, 'E' => 5],
 ];

Solution

  • Seems like you're trying to convert array string to an array.

    You can repeat through loop or make function to get your desired output.

    I'm using regular expression with preg_match_all

    Code

    $rawArray  =  array("A"=>"['B' => 3, 'C' => 5, 'D' => 9],",
        "B"=>"['A' => 3, 'C' => 3, 'D' => 4, 'E' => 7],",
        "C"=>"['A' => 5, 'B' => 3, 'D' => 2, 'E' => 6, 'F' => 3],",
        "D"=>"['A' => 9, 'B' => 4, 'C' => 2, 'E' => 2, 'F' => 2],",
        "E"=>"['B' => 7, 'C' => 6, 'D' => 2, 'F' => 5],",
        "F"=>"['C' => 3, 'D' => 2, 'E' => 5],",
    );
    foreach($rawArray as $k => $v){
    preg_match_all("/\'(.)\'/", $v, $key);
    preg_match_all("/=> (\d)/", $v, $val);
    $graph[$k] = array_combine($key[1], $val[1]);
    }
    print_r($graph);
    

    Output

    Array
    (
        [A] => Array
            (
                [B] => 3
                [C] => 5
                [D] => 9
            )
    
        [B] => Array
            (
                [A] => 3
                [C] => 3
                [D] => 4
                [E] => 7
            )
    
        [C] => Array
            (
                [A] => 5
                [B] => 3
                [D] => 2
                [E] => 6
                [F] => 3
            )
    
        [D] => Array
            (
                [A] => 9
                [B] => 4
                [C] => 2
                [E] => 2
                [F] => 2
            )
    
        [E] => Array
            (
                [B] => 7
                [C] => 6
                [D] => 2
                [F] => 5
            )
    
        [F] => Array
            (
                [C] => 3
                [D] => 2
                [E] => 5
            )
    
    )
    

    Live demo

    Explanation:

    1. $rawArray is associate array, each of it's element contain string similar to php array.

    2. We're looping through array and converting that string to array by using preg_match_all and building $graph multidimension array.

    3. When loop execute first time $k is equal to A and $v is equal to ['B' => 3, 'C' => 5, 'D' => 9],

    4. First preg_match_all make array of keys from $v (['B' => 3, 'C' => 5, 'D' => 9],), and assign it to $key[1]. Now $key[1] is array ['B', 'C', 'D'].

    5. Second preg_match_all make array of values from $v (['B' => 3, 'C' => 5, 'D' => 9],), and assign it to $val[1]. Now $val[1] is array [2, 5, 9].

    6. We're combining$key[1]as keys and $val[1] as values by using array_combine to the $graph[$k] where $k is A.

    How preg_match_all works?

    preg_match_all($pattern, $string, $out);
    

    It's matches pattern from string and then assign result to the $out as array.

    Learn more about.
    preg_match_all
    regex pattern cheat sheet

    Note: We're using non-capturing pattern so, it's return both exact match and desired match... So our desired record found in$key[1].