Looping through multidimensional array in CodeIgniter Looping through multidimensional array in CodeIgniter codeigniter codeigniter

Looping through multidimensional array in CodeIgniter


You can add your week data to an array during your for loop, identified by the campaign ID as the array key, then pass that to your view:

foreach($data['campaigns'] as $item) {    for($x = 0; $x < 7; $x++) {        $data['CampaignData'][$item['CampaignId']][] = $this->model_record->week_data($x, $item, $StartDate);    }}

Then you can output your data like this:

foreach($data['CampaignData'] as $campaign_id => $week_data) {    echo 'Campaign: ' . $campaign_id . ', web hits: ' . implode(', ', $week_data) . PHP_EOL;}

You can of course format your output better than that, but that example should give you what you want, e.g.:

Campaign: 1, web hits: 55, 63, 32, 39, 22, 33, 61


Your for loop statement looks incorrect. If you want to loop 7 times, change it to

for($i = 0; $i <= 6; $i++)

Your final code should probably look like this

foreach($data['campaigns'] as $item) {    $data[$item['CampaignId']] = array();    for($x = 0;$x <= 6;$x++) {        $data[$item['CampaignId']][] = $this->model_record->week_data($x,$item,$StartDate);    }}