2

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,
 ],
]
13
  • 2
    Looks good ... oh, wait, do you have a question? Commented Jan 19, 2018 at 6:52
  • 2
    Could you please read how to ask guide and elaborate more on your question? Commented Jan 19, 2018 at 6:52
  • 2
    $array = explode("\n", $string) Commented Jan 19, 2018 at 6:52
  • 1
    If you did read that, then where is the code u've already tried? Commented Jan 19, 2018 at 6:53
  • 1
    @Kenn I doubt it. Because this isn't how do you normally ask your question. Could you show us what have you tried already? Where did you fail to convert it? Any piece of code you already wrote and need help with? Commented Jan 19, 2018 at 6:54

1 Answer 1

3

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
        )

)
Sign up to request clarification or add additional context in comments.

14 Comments

OP itself said:- So far I have tried, explode() ->$response['message'] = explode("\n", $response['message']);
How can I show the result and share it with you guys? The code returns an array of string. First array is "\"ADGROUP_PERFORMANCE_REPORT (Jan 12, 2018-Jan 18, 2018)\"" second array is "Ad group ID,Ad group,Ad group state,Campaign ID,Campaign,Campaign state"
@Kenn Isn't that what you want? An array of string? I'll update my answer with my full code and output.
@Kenn yeah, we need to explode them again.
@Kenn Updated Answer to output the desired result you want, you can use that to create a new array if you would like.. Depends on your final goal
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.