updating values in an array with foreach ? how

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
99teddy99
Forum Newbie
Posts: 1
Joined: Sun Oct 31, 2010 9:06 am

updating values in an array with foreach ? how

Post by 99teddy99 »

Hi there from a newby,

I really need a pointer please with regard to updating an array, I think I'm stuck with the $key bit, pass by value, pass by reference thing..

I have an array called $case_fees which is built by this select:
SELECT
cases_fees.updated_on as 'Entered on',
cases_fees.eff_date as 'Effective on',
CONCAT(peeps.forename, ' ', peeps.surname) AS `Updated by`,
CONCAT(cases_fees.fee_type, ' ', fee_types.text) AS `Fee`,
cases_fees.approved,
cases_fees.amount,
CONCAT(assigned.forename, ' ', assigned.surname) AS `Assigned to`........ etc...

I want to loop through the array, and if the 'amount' is 0 then I want to say (for simlicity) set it to 5.

so I attempted something like this:

if ($case_fees) {
foreach ($case_fees as $fee) {
if ($fee['amount'] == 0) { $fee['amount'] = 5; }
}
}

If someone could please show me how to amend this code so that it will update the valud back into the array for later use, that will save me a headache, I've already been trying for hours to get it to work.

Thanks :-)
mikecampbell
Forum Commoner
Posts: 38
Joined: Tue Oct 12, 2010 7:26 pm

Re: updating values in an array with foreach ? how

Post by mikecampbell »

This should do it.

Code: Select all

if ($case_fees) {
  foreach ($case_fees as $key=>$fee) {
    if ($fee['amount'] == 0) { $case_fees[$key]['amount'] = 5; }
  }
}
Post Reply