0

I have the following data structure:

$campaigns =
Array
(
[0] => Array
    (
        [subject] => cca-cpg
    )

[1] => Array
    (
        [subject] => cleanup-cpg
    )

[2] => Array
    (
        [subject] => gas-cpg
    )

[3] => Array
    (
        [subject] => pollinators-cpg
    )

)

what I would like to end up with is:

$campaigns = ['cca-cpg','clean_up-cpg','gas-cpg','pollinators-cpg'];


this will work:
$newCampaigns=[];
for($i=0;$i<count($campaigns);$i++){
   array_push($newCampaigns,$campaigns[$i]['subject'];
}

but I was wondering if there's a better way to do this. The data is coming directly from a mysql database

0

1 Answer 1

2

There's array_column() function for you:

$newCampaigns = array_column($campaigns, 'subject');

Source: https://www.php.net/manual/en/function.array-column.php

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.