php - Define an element of an array using values of other elements in the same array -


i want define element in single dimensional array (e.g.) sum of 2 other elements.

i have tried this:

$results = array( 0 => 5, 1 => 10, 2 => $results[0] + $results[1] ); 

but $results[2] returns empty.

what correct way reference other elements of current array?

this abstraction of more situation - general need able define elements function of sibling elements.

you first have assign other 2 values, otherwise can't access them until assigned variable.

so this:

$results = [5, 10]; $results[] = $results[0] + $results[1]; print_r($results); 

output:

array ( [0] => 5 [1] => 10 [2] => 15 )