转换包含多维数组键和值PHP的字符串

时间:2016-08-26 03:19:17

标签: php arrays regex stdclass recursive-backtracking

我使用TPP ... "dependencies": { "@angular/common": "2.0.0-rc.5", "@angular/compiler": "2.0.0-rc.5", "@angular/core": "2.0.0-rc.5", "@angular/forms": "0.3.0", "@angular/http": "2.0.0-rc.5", "@angular/platform-browser": "2.0.0-rc.5", "@angular/platform-browser-dynamic": "2.0.0-rc.5", "@angular/platform-server": "2.0.0-rc.5", "@angular/router": "3.0.0-rc.1", "bootstrap": "^4.0.0-alpha.2", "core-js": "^2.4.1", "font-awesome": "^4.6.3", "jquery": "^3.1.0", "lodash": "^4.15.0", "moment": "^2.14.1", "ng2-bootstrap": "^1.0.24", "rxjs": "5.0.0-beta.11", "tether": "^1.3.4", "zone.js": "^0.6.17" }, "devDependencies": { "@types/jasmine": "^2.2.33", "@types/lodash": "4.14.34", "@types/node": "^6.0.38", "@types/source-map": "^0.1.26", "@types/webpack": "^1.12.33", "angular2-hmr": "~0.8.1", "angular2-template-loader": "^0.5.0", "autoprefixer": "^6.4.0", "awesome-typescript-loader": "2.2.1", "bootstrap-loader": "^2.0.0-beta.3", "codelyzer": "0.0.28", "css-loader": "^0.24.0", "extract-text-webpack-plugin": "^2.0.0-beta.3", "file-loader": "^0.9.0", "font-awesome-sass-loader": "^1.0.2", "html-loader": "^0.4.3", "html-webpack-plugin": "^2.22.0", "http-server": "^0.9.0", "imports-loader": "^0.6.5", "istanbul-instrumenter-loader": "^0.2.0", "jasmine-core": "^2.4.1", "json-loader": "^0.5.4", "karma": "^1.2.0", "karma-chrome-launcher": "^2.0.0", "karma-coverage": "^1.1.1", "karma-jasmine": "^1.0.2", "karma-mocha-reporter": "^2.1.0", "karma-sourcemap-loader": "^0.3.7", "karma-webpack": "^1.8.0", "node-sass": "^3.8.0", "postcss-loader": "^0.10.1", "raw-loader": "^0.5.1", "remap-istanbul": "^0.6.4", "resolve": "^1.1.7", "resolve-url-loader": "^1.6.0", "rimraf": "^2.5.4", "sass-loader": "^4.0.0", "source-map-loader": "^0.1.5", "style-loader": "^0.13.1", "to-string-loader": "^1.1.4", "ts-helpers": "1.1.1", "ts-node": "^1.3.0", "tslint": "^3.15.1", "tslint-loader": "^2.1.5", "typescript": "^2.0.0-beta", "url-loader": "^0.5.7", "webpack": "^2.1.0-beta.21", "webpack-dev-server": "^2.1.0-beta.0", "webpack-merge": "^0.14.1" } ... 来检查域可用性和域名注册,但我在API收到回复。

  1. 获取会话,返回字符串string

  2. 域名检查,返回字符串OK: t73484678463765

  3. 在其他情况下,返回字符串woohoo123.nz: OK: Minimum=1&Maximum=2

  4. 当它返回woohoo123.nz: ERR: 102, This is message时,其中OK&ERR该时间为,

    我希望将string转换为array

    例如输入woohoo123.nz: OK: Minimum=1&Maximum=2array

    之后的输出
     [
         'woohoo123.nz' => [
             'OK' => [
                 'Minimum' => 1,
                 'Maximum' => 2,
             ]
         ]
     ]
    

    输入woohoo123.nz: ERR: 102, This is message并输出array

     [
         'woohoo123.nz' => [
             'ERR' => [
                 'code' => 102,
                 'message' => 'This is message',
             ]
         ]
     ]
    

    我更喜欢重复使用代码,我更喜欢recursivecallback,但在这种情况下不确定。

1 个答案:

答案 0 :(得分:1)

不是100%确定这是否是您正在寻找的。它适用于您的示例,但只有在输入字符串严格遵循该格式时才会继续有效。

    function stringToArray($inputStr) {
    $array = [];

    $topComponents = explode(': ',$inputStr);
    $parametersStr = $topComponents[count($topComponents) -1];
    if (strpos($parametersStr,'&') !== FALSE) {
        $tmpArr = explode('&',$parametersStr);
        foreach ($tmpArr as $val) {
            $comp = explode('=',$val);
            $array[$comp[0]] = $comp[1];
        }
    } else if ($topComponents[count($topComponents) - 2] === "ERR") {
        $tmpArray = explode('ERR: ',$parametersStr);
        $tmpArray = explode(', ',$tmpArray[0]);
        $array = [
            "code" => intval($tmpArray[0]),
            "message" => $tmpArray[1]
        ];
    } else {
        $array = $parametersStr;
    }

    for ($i=count($topComponents) -2; $i >= 0; $i--) {
        $newArray = [];
        $newArray[$topComponents[$i]] = $array;
        $array = $newArray;
    }

    return $array;
}

print_r(stringToArray("OK: t73484678463765"));