Would it be possible to combine an arrays keys to create a new key using the combined key values?
I’m not asking to create a function to combine them, just wondering if it is possible to do something like this (obviously this code doesn’t work, just showing what I meant in code):
<?php
$box = array(
"Width" => 10,
"Height" => 20,
"Total" => ($box["Width"] + $box["Height"]),
);
echo $box["Total"]; // would show up as 30
?>
No, not while the array is being defined.
array(...)is being evaluated first, the result of which is assigned=to$box. You can’t refer to$boxbefore the evaluation is finished.You’ll have to do it in two steps, or perhaps create a custom class that can do such magic using methods and/or (automagic) getters.