Parse Nonstandard API Response into an Array

Holo :

I have a query that gets data from an API, this is an example of what it returns.

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

I want to convert this into some form of a usable array, pass it into a view as a table.

What is confusing me more is that the user name at the start is just the username, no key, just the value of username. Not sure how to convert this to an array and then loop through to output it.

This is the first two lines of raw output from the API return.

user1=bandwidth=7.05 / 128000&creator=mtemtfqx&date_created=1582530762&default=domain1.com&email_daily_limit=1000&email_deliveries_outgoing=0&inode=1492 / unlimited&ip=139.99.69.103&ips=139.99.69.103 &list=domain1.com &package=shared5"a=32.2 / 5120&suspended=No&type=user&vdomains=1 / 5 user2=bandwidth=1.46 / 128000&creator=mtemtfqx&date_created=1583765836&default=domain2.net&email_daily_limit=1000&email_deliveries_outgoing=1&inode=2355 / unlimited&ip=139.99.69.103&ips=139.99.69.103 &list=domain2.net

Nigel Ren :

This splits the input string by an =, but only 2 parts, the first part is the user, the second part are the values. Then using parse_str() it decodes the values.

These are put into an array with the user name as the key...

$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);

gives the output of...

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
        )

)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=346501&siteId=1