zacknafein
zacknafein

Reputation: 50

Change the value in a multidimensionnal array with foreach in php

I have a multidimensional array that come from a MySQL table with 8 column. 2 of them are dates, and I want to update one of those by adding 1 week. Here is what I did :

foreach ($datas as &$data_rows) {
    if($data_rows[3] == "Each week"){
        $date = strtotime($data_rows[6]);
        $data_rows[1] = date("Y-m-d", strtotime("+1 week", $date));
    }
}

There is a lot of job with many different frequencies, so what I'm trying to do is first, add to the last time the job have been done the frequency (+ 1 week, +1 month, etc.) and doing that with that example here (and some else if for the other frequency), so I need to update the dates in 1 column

Then I want to sort those date so the first lane will be the first job to do. but for that, I need to update a column of my array.

It's just not working, if I do stuff in the foreach, it's ok, but as soon the foreach is over, I can't uses it anymore, the data are not saved...

Upvotes: 0

Views: 50

Answers (1)

Joseph_J
Joseph_J

Reputation: 3669

I think it would be easiest to use a for loop and just reference you indexes with the counter.

Providing your dates that you are trying to convert are a legitimate date string then this should work for you.

for($i = 0; $i < count($data); $i++) {
    if($data[$i][3] == 'Each week') {
        $data[$i][1] = date('Y-m-d', strtotime($data[$i][6] . ' +1 week'));
    }
}

Upvotes: 1

Related Questions