パース非標準API応答配列に

ホロ:

私は、これはそれが返すものの一例であり、APIからデータを取得するクエリを持っています。

testuser=bandwidth=36.4/128000&domain=test.com&[email protected]&inodes=1566
testuser=bandwidth=36.4/128000&domain=test.com&[email protected]&inodes=1566
testuser=bandwidth=36.4/128000&domain=test.com&[email protected]&inodes=1566

私は、使用可能な配列のいくつかのフォームにこれを変換するテーブルとしてビューにそれを渡します。

何より、私を混乱さは、開始時に、そのユーザー名であるだけで、ユーザー名、ノーキー、ユーザ名の値だけです。わからないことが出力に至るアレイと、ループにこれを変換する方法。

これは、APIの戻りからの生の出力の最初の2行です。

USER1 =帯域幅= / 128000&クリエイター7.05 = mtemtfqx&DATE_CREATED = 1582530762&デフォルト= domain1.com&email_daily_limit 1000&email_deliveries_outgoing = 0&iノード= 1492 /無制限&IP = 139.99.69.103&IPS = 139.99.69.103&リスト= domain1.com&パッケージ= shared5" = 32.2 / 5120&一時停止=いいえ&タイプ=ユーザー&vdomainsを= = 1月5日user2の帯域幅= = / 128000&1.46クリエーター= mtemtfqx&DATE_CREATED = 1583765836&デフォルト= domain2.net&email_daily_limit 1000年&email_deliveries_outgoing = 1&iノード= = 2355 /無制限&IP = 139.99.69.103&IPS = 139.99.69.103&リスト= domain2.net

ナイジェルレン:

これにより、入力文字列を分割し=たが、わずか2部分は、最初の部分は、ユーザである、第二の部分は、値です。そして、使用してparse_str()、それが値をデコードします。

これらは、キーとしてユーザー名を持つ配列に入れています...

$output = [];
$data = 'testuser=bandwidth=36.4/128000&domain=test.com&[email protected]&inodes=1566
testuser1=bandwidth=36.41/128000&domain=test.com&[email protected]&inodes=1566
testuser2=bandwidth=36.42/128000&domain=test.com&[email protected]&inodes=1566';

foreach ( explode(PHP_EOL, $data ) as $line )   {
    $lineData = explode("=", $line, 2);
    if ( isset($lineData[1]) )  {
        parse_str($lineData[1], $userData );
        $output [ $lineData[0] ] = $userData;
    }
}

print_r($output);

出力を提供します...

Array
(
    [testuser] => Array
        (
            [bandwidth] => 36.4/128000
            [domain] => test.com
            [email] => [email protected]
            [inodes] => 1566
        )

    [testuser1] => Array
        (
            [bandwidth] => 36.41/128000
            [domain] => test.com
            [email] => [email protected]
            [inodes] => 1566
        )

    [testuser2] => Array
        (
            [bandwidth] => 36.42/128000
            [domain] => test.com
            [email] => [email protected]
            [inodes] => 1566
        )

)

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=347398&siteId=1