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条)

报告相同问题?

悬赏问题

  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面