how do I update the value in itemprices if iteminfo equals to certain subject?
User(
[name] => xxxx
[phone] => xxxxx
[email]xxxxx
[itemprices] => Array ( [0] => 1.00 [1] => 1.00 )
[iteminfo] => Array ( [0] => Chemistry [1] => Biology )
)
One possible approach:
$subject = 'Chemistry';
$index = array_search($subject, $user->iteminfo);
if (false !== $index) {
$user->itemprices[$index] = $newvalue;
}
Explanation: first you use array_search to find the index of the given subject in iteminfo
array. If it's indeed there, you use that index to update the corresponding value in itemprices
.
But actually, I'd rather rearrange the data into associative array where all the keys correspond to the subjects, and all the values - to their prices respectively. It's quite easy to do with array_combine:
$user->itemdata = array_combine($user->iteminfo, $user->itemprices);
Then you won't have to use array_search
any more, accessing the price info directly by the subject key:
$user->itemdata[$subject] = $newvalue;