I'm looping through an array using a foreach
loop:
$attributes = array( 'drink', 'price', 'amount' );
foreach ( $attributes as $key ) {
$info[$key] = $key;
}
How can I type cast my $key
variable differently each time the loop runs? For example, I need drink to be type string
, price to be float
and amount to be int
.
Please note, this is a simplified example of my code. Type casting the keys before the loop is run isn't suitable in my case.
I tried the following but it didn't seem to work:
$attributes = array( 'drink' => 'string', 'price' => 'float', 'amount' => 'int' );
foreach ( $attributes as $key => $value ) {
$info[$key] = ($value) $key;
}