如何将此String转换为Array?

时间:2018-01-19 06:50:46

标签: php data-conversion

我有来自Google Adwords的数据。它返回一个字符串。

"\"ADGROUP_PERFORMANCE_REPORT (Jan 11, 2018-Jan 17, 2018)\"\nAd group ID,Ad group,Ad group state,Campaign ID,Campaign,Campaign state\n47069225942,Conversse,enabled,1002620524,Blizzard Entertainment #5a20e1cd04140,enabled\n49685186135,Starcraft II #5a20e20434e4b,enabled,1002620524,Blizzard Entertainment #5a20e1cd04140,enabled\n52639270027,midnight test,enabled,1002620524,Blizzard Entertainment #5a20e1cd04140,enabled\n50529526715,one,enabled,1010615735,December,enabled\n50530330478,Don't Remove ;),enabled,823386372,BS Test Campaign,enabled\n52012184360,Don't Remove ;),enabled,869044770,BruceTesting,enabled\n50530808398,SAMPLE KEN 2 #5a1fb34fb81c8,enabled,999354939,lazada,enabled\n53247341001,ken 4,paused,999354939,lazada,enabled\n53451857190,SAMPLE KEN 3 #5a1fc416984ce,enabled,999354939,lazada,enabled\n54964303332,SAMPLE KEN 1 #5a1fb3321af4c,enabled,999354939,lazada,enabled\n57947888068,ken5,enabled,999354939,lazada,enabled\n52357571231,Beyond Science Free Trial,enabled,1007003986,Gavin Testing,paused\nTotal, --, --, --, --, --\n"

这就是它的样子。 enter image description here

到目前为止,我已尝试过,explode()

$response['message'] = explode("\n", $response['message']);
Returns::json($response);
{
    "status": "success",
    "message": [
        "\"ADGROUP_PERFORMANCE_REPORT (Jan 11, 2018-Jan 17, 2018)\"",
        "Ad group ID,Ad group,Ad group state,Campaign ID,Campaign,Campaign state",
        "50530330478,Don't Remove ;),enabled,823386372,BS Test Campaign,enabled",
        "52012184360,Don't Remove ;),enabled,869044770,BruceTesting,enabled",
        "50530808398,SAMPLE KEN 2 #5a1fb34fb81c8,enabled,999354939,lazada,enabled",
        "53247341001,ken 4,paused,999354939,lazada,enabled",
        "53451857190,SAMPLE KEN 3 #5a1fc416984ce,enabled,999354939,lazada,enabled",
        "54964303332,SAMPLE KEN 1 #5a1fb3321af4c,enabled,999354939,lazada,enabled",
        "57947888068,ken5,enabled,999354939,lazada,enabled",
        "52357571231,Beyond Science Free Trial,enabled,1007003986,Gavin Testing,paused",
        "47069225942,Conversse,enabled,1002620524,Blizzard Entertainment #5a20e1cd04140,enabled",
        "49685186135,Starcraft II #5a20e20434e4b,enabled,1002620524,Blizzard Entertainment #5a20e1cd04140,enabled",
        "52639270027,midnight test,enabled,1002620524,Blizzard Entertainment #5a20e1cd04140,enabled",
        "50529526715,one,enabled,1010615735,December,enabled",
        "Total, --, --, --, --, --",
        ""
    ]
}

这是返回类:

class Returns
{
    static function json($data)
    {
        header('Content-type: application/json');
        echo json_encode($data);
    }
}

如何将此String转换为数组?

我想得到的预期结果是:

[
 [
  AdGroupID: 12323123,
  AdGroup: Converse,
  AdGroupState: enabled,
  CampaignID: 12345,
  Camoaign: blizzard,
  CamoaignState: enabled,
 ],
 [
  AdGroupID: 12323123,
  AdGroup: Converse,
  AdGroupState: enabled,
  CampaignID: 12345,
  Camoaign: blizzard,
  CamoaignState: enabled,
 ],
]

1 个答案:

答案 0 :(得分:3)

由于您的$response['message']包含您要转换为数组的字符串,因此您可以使用claypoole

$exploded_string_array = explode("\n", $response['message']);
print_r($exploded_string_array); //to dump the array to the screen and u see how it was exploded.

这将允许您使用explode function

访问阵列
foreach ($exploded_string_array as $key => $value) {
    echo $key ." ". $value;
}

<强>更新

在您更新问题并使其更清晰之后,这是获得所需输出的一种方法:

<?php
    //Updated the code to use array built-in functions instead of manually setting the keys.
    $stringToExplode = "\"ADGROUP_PERFORMANCE_REPORT (Jan 11, 2018-Jan 17, 2018)\"\nAd group ID,Ad group,Ad group state,Campaign ID,Campaign,Campaign state\n47069225942,Conversse,enabled,1002620524,Blizzard Entertainment #5a20e1cd04140,enabled\n49685186135,Starcraft II #5a20e20434e4b,enabled,1002620524,Blizzard Entertainment #5a20e1cd04140,enabled\n52639270027,midnight test,enabled,1002620524,Blizzard Entertainment #5a20e1cd04140,enabled\n50529526715,one,enabled,1010615735,December,enabled\n50530330478,Don't Remove ;),enabled,823386372,BS Test Campaign,enabled\n52012184360,Don't Remove ;),enabled,869044770,BruceTesting,enabled\n50530808398,SAMPLE KEN 2 #5a1fb34fb81c8,enabled,999354939,lazada,enabled\n53247341001,ken 4,paused,999354939,lazada,enabled\n53451857190,SAMPLE KEN 3 #5a1fc416984ce,enabled,999354939,lazada,enabled\n54964303332,SAMPLE KEN 1 #5a1fb3321af4c,enabled,999354939,lazada,enabled\n57947888068,ken5,enabled,999354939,lazada,enabled\n52357571231,Beyond Science Free Trial,enabled,1007003986,Gavin Testing,paused\nTotal, --, --, --, --, --\n";

    $exploded_string = explode("\n", $stringToExplode);
    $inner_exploded_array = [];

    foreach ($exploded_string as $key => $value) {
        array_push($inner_exploded_array, explode(",", $value));
    }

    $final_array = [];

    for ($j=2;$j<count($inner_exploded_array)-2;$j++) {
        $inner_array = array_combine($inner_exploded_array[1], $inner_exploded_array[$j]);
        array_push($final_array, $inner_array);
    }

    echo "<pre>";
    print_r($final_array);
    echo "</pre>";
?>

输出:

Array
(
    [0] => Array
        (
            [Ad group ID] => 47069225942
            [Ad group] => Conversse
            [Ad group state] => enabled
            [Campaign ID] => 1002620524
            [Campaign] => Blizzard Entertainment #5a20e1cd04140
            [Campaign state] => enabled
        )

    [1] => Array
        (
            [Ad group ID] => 49685186135
            [Ad group] => Starcraft II #5a20e20434e4b
            [Ad group state] => enabled
            [Campaign ID] => 1002620524
            [Campaign] => Blizzard Entertainment #5a20e1cd04140
            [Campaign state] => enabled
        )

    [2] => Array
        (
            [Ad group ID] => 52639270027
            [Ad group] => midnight test
            [Ad group state] => enabled
            [Campaign ID] => 1002620524
            [Campaign] => Blizzard Entertainment #5a20e1cd04140
            [Campaign state] => enabled
        )

    [3] => Array
        (
            [Ad group ID] => 50529526715
            [Ad group] => one
            [Ad group state] => enabled
            [Campaign ID] => 1010615735
            [Campaign] => December
            [Campaign state] => enabled
        )

    [4] => Array
        (
            [Ad group ID] => 50530330478
            [Ad group] => Don't Remove ;)
            [Ad group state] => enabled
            [Campaign ID] => 823386372
            [Campaign] => BS Test Campaign
            [Campaign state] => enabled
        )

    [5] => Array
        (
            [Ad group ID] => 52012184360
            [Ad group] => Don't Remove ;)
            [Ad group state] => enabled
            [Campaign ID] => 869044770
            [Campaign] => BruceTesting
            [Campaign state] => enabled
        )

    [6] => Array
        (
            [Ad group ID] => 50530808398
            [Ad group] => SAMPLE KEN 2 #5a1fb34fb81c8
            [Ad group state] => enabled
            [Campaign ID] => 999354939
            [Campaign] => lazada
            [Campaign state] => enabled
        )

    [7] => Array
        (
            [Ad group ID] => 53247341001
            [Ad group] => ken 4
            [Ad group state] => paused
            [Campaign ID] => 999354939
            [Campaign] => lazada
            [Campaign state] => enabled
        )

    [8] => Array
        (
            [Ad group ID] => 53451857190
            [Ad group] => SAMPLE KEN 3 #5a1fc416984ce
            [Ad group state] => enabled
            [Campaign ID] => 999354939
            [Campaign] => lazada
            [Campaign state] => enabled
        )

    [9] => Array
        (
            [Ad group ID] => 54964303332
            [Ad group] => SAMPLE KEN 1 #5a1fb3321af4c
            [Ad group state] => enabled
            [Campaign ID] => 999354939
            [Campaign] => lazada
            [Campaign state] => enabled
        )

    [10] => Array
        (
            [Ad group ID] => 57947888068
            [Ad group] => ken5
            [Ad group state] => enabled
            [Campaign ID] => 999354939
            [Campaign] => lazada
            [Campaign state] => enabled
        )

    [11] => Array
        (
            [Ad group ID] => 52357571231
            [Ad group] => Beyond Science Free Trial
            [Ad group state] => enabled
            [Campaign ID] => 1007003986
            [Campaign] => Gavin Testing
            [Campaign state] => paused
        )

)