4

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?

0

2 Answers 2

9

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?

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

Comments

6

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.