2

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
?>

4 Answers 4

3

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 $box before 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.

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

2 Comments

Lol, not sure why other solutions are being posted. This response answered the OP in a clear and concise manner.
Thanks for the answer, I searched for the thing I asked but couldn't find it. But you answer cleared things up for me, thanks.
2

You need 2 steps:

$box = array(
    "Width" => 10,
    "Height" => 20,
);
$box["Total"] = $box["Width"] + $box["Height"];
echo $box["Total"];

2 Comments

I like the code, but an explanation that details that the key/value pairs are parsed before being assigned to $box would make this answer a little more complete in my eyes.
Combine this with deceze's answer.
1

The easy answer is no.

To elaborate: this is precisely what classes are meant to do. Note that you can do what you are trying to do very simply:

<?php
class Box extends ArrayObject
{
  public function offsetGet($key)
  {
    return $key == 'Total' ? $this['Width'] + $this['Height'] : parent::offsetGet($key);
  }
}

$box = new Box(array(
  'Width' => 10,
  'Height' => 20
));

echo $box['Total'],"\n";

Of course $box is not a true array in this example, and as such, cannot directly be used with array functions. See the docs for ArrayObject.

2 Comments

How is this simple, at least compared to the other answers?
@showerhead: this is even simpler: $w = 10; $h = 20; $total = $w + $h;, but that doesn't mean one shouldn't use arrays. My answer is simply to illustrate a concept: using classes to compute derived values doesn't necessarily involve a lot of code. If you are creating boxes across the entire site, setting up a class to handle them is less error prone because you only have to do it once. In practice, I wouldn't actually use an ArrayObject for this; a standard class would do. And of course if this is just a simple, one time procedure, then I'd do exactly what xdazz has suggested.
-1

Since you already have 10 and 20 you can write

<?php
$box = array(
    "Width" => 10,
    "Height" => 20,
    "Total" => 10 + 30,
);
echo $box["Total"]; // would show up as 30

1 Comment

Bad coding style to define values more than once. Are the 10 in Width and the 10 in Total related? Hard to tell if I didn't write the code. And VERY hard to tell when Height is 20 and then we've got 30. What's 30? Your comment is also wrong. 10 + 30 would show up as 40.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.