dqvrlgi3247 2018-07-19 21:10
浏览 16
已采纳

将数组值转换为数组

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],
 ];
  • 写回答

4条回答 默认 最新

  • dongzhu0327 2018-07-19 22:15
    关注

    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].

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?