0

Is it possible to push a value into array based on its key but actually add to the key.

For example

$data = array();

    $data[0]= 12;
    $data[1]= 1;
    $data[2]= 2;
    $data[3]= 56;
    $data[4]= 78;

array_push($data, 0,23);

so the output would be

$data[0]= 35;   (12+23)
$data[1]= 1;
$data[2]= 2;
$data[3]= 56;
$data[4]= 78;
0

2 Answers 2

4

Why not $data[0] += 23?

Or

Why not $data[0] = $data[0] + 23?

Sign up to request clarification or add additional context in comments.

Comments

3

Instead of using =, you can use += to append a value to a variable:

$data[0] += 23;

This code is equivalent to this:

$data[0] = $data[0] + 23;

You can see the output here: http://codepad.org/bNruNNFe

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.