0

For an array like:

Array1 ( [a] => 1 [b] => 2 [c] => 7 [d] => )

I would like to update certain values if keys with variable match.

For example, I am have two variables (a=0.5 and c=0.25). I may also have one or more than two variables which would need to be added to the array.

In the case where I have two variables (a=0.5 and c=0.25).

Can one update the array by adding the proper variable to its corresponding key in the array?

End result:

Array1 ( [a] => 1.5 [b] => 2 [c] => 7.25 [d] => )

Can't think of a way to add values to original array (be from single array, multidimensional array or variables where the key match). I am a newbie to adding or updating array's key value, and am super stuck at these right now. Trying all kinds of things. Thank you!

PS. I am not playing around with converting the original array to variables, perform the math, then convert back again to single dimensional array, but it will not do since the array may have unique keys every time... hmmm...

FartMachine4U
  • 117
  • 1
  • 3
  • 10

1 Answers1

1

Basically what you want to do is add the values to the array by referencing a key name of a, b etc. You should just be able to do this:

$array['a'] += $value_to_add;

... but you might get undefined index errors if the variable doesn't exist already. So the best way to do it would be to set the key if it doesn't exist already:

if(!array_key_exists('a', $array))
    $array['a'] = 0;

$array['a'] += $new_value;

Now, if you're talking about having an array like your example:

Array1 ( [a] => 1 [b] => 2 [c] => 7 [d] => )

...and these variables:

$a = 0.5;
$c = 0.25;

...and wanting to arbitrarily adding unknown variable names to their respective keys in your array, you'll need to get the variable name as a string so that you can search for it in your array.

If this is the case, you'd be better off putting the variables to add into an array:

$vars_to_add = array(
    'a' => 0.5,
    'c' => 0.25
);

...then you can do a simple loop over the new array to add them to the original array:

foreach($vars_to_add as $key => $current) {
    if(!array_key_exists($key, $original_array))
        $original_array[$key] = 0; // initialize blank variable

    // add new value to original array
    $original_array[$key] += $current;
}

End result of $original_array would look like this:

Array1 ( [a] => 1.5 [b] => 2 [c] => 7.25 [d] => )
Community
  • 1
  • 1
scrowler
  • 24,273
  • 9
  • 60
  • 92
  • scrowler - I think this would do. The 'array_key_exists' is critical here. I would pressume that one can expand on the if/else condition inside the foreach, and adjust it for other calculations? I am thinking of adding multiple if/elseif/else (so that if a certain key if of a certain value, perform addition, if not then perform substraction. Greatly appreciated scrowler! – FartMachine4U Jan 27 '14 at 00:07
  • Of course, you could do anything inside that statement. You'd just need to work out how to differentiate between which to do. Easiest way would probably be have different arrays (keep in mind that `+= -0.5` will actually subtract 0.5) for addition/subtraction, multiplication and division etc – scrowler Jan 27 '14 at 00:13
  • scrowler - say, I want to add another condition in the foreach, where I perform a substraction of a value if key with name of x exists in $original_array; I added: if(array_key_exists('x', $original_array)) but nothing is happening; I am debugging this to see where are the values and if I am sending this correct – FartMachine4U Jan 27 '14 at 01:17
  • I added "if(array_key_exists($other_key_name, $original_array))" to check for other key and perform other calculation, but it was added outside the foreach loop. The "array_key_exists" is key, and helped. Thank you. – FartMachine4U Jan 27 '14 at 01:56