KD.S.T.
KD.S.T.

Reputation: 603

How to convert this String to Array?

I have a data from Google Adwords. It returns a string.

"\"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"

This is what it looks like. enter image description here

So far I have tried, 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, --, --, --, --, --",
        ""
    ]
}

This is the Return Class:

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

How to convert this String to Array?

The expected result what I want to get is:

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

Upvotes: 2

Views: 122

Answers (1)

Paul Karam
Paul Karam

Reputation: 4225

Since your $response['message'] holds the string that you would like to convert to an Array, you can use explode function:

$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.

This will allow you to access the array using foreach:

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

Update:

After you updated your question and made it much clearer, here's a one way to get to your desired output:

<?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>";
?>

Output:

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
        )

)

Upvotes: 3

Related Questions