I have this foreach here
<?php foreach($division as $value){
$arraydivision[] = $value['name'];
} ?>
but the keys come back as 0, 1, 2, 3, 4
I would like the keys to be also the names...I have tried
<?php foreach($division as $value){
$arraydivision[] = $value['name'] => $value['name'];
} ?>
But that didnt work, just gave me an error...anyone know why this is not working?
Assuming $value['name']
is the name you want:
foreach($division as $value){
$arraydivision[$value['name']] = $value['name'];
}
print_r($arraydivision);
Note: Seems odd to assign key and value the same. Maybe you wish to assign $value
?
The PHP syntax for this is:
$arraydivision[$value['name']] = $value['name'];
Take a look at PHP array documentation, section Creating/modifying with square bracket syntax
, there are also exmpales on how to use unset()
and other details.
You also may find documentation for foreach
interesting (especially secion on array_expression as $key => $value
syntax).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With