oderfla
oderfla

Reputation: 1797

How to read comment inside an array

Lets say I have this array:

$arr = [
    "UK" #United Kingdom
    ,"USA" #United States
    ,"BR" #Brazil
];

Is it possible to loop through the array AND a part from the value itself also get the comment? Of course I could make this to a key/value array. But as there are so many entries I wonder if I can take advantage of the comments.

Basically I would need to do:

foreach($arr as $key=>value) {
    echo 'Code: ' . $key . PHP_EOL;
    echo 'Name: ' . $value . PHP_EOL;
}

Doing this today it is of course giving me the array index as key and the code as value.

Upvotes: 0

Views: 117

Answers (1)

Dokik
Dokik

Reputation: 364

You need to define your array as following

$arr = [
"UK" => "United Kingdom",
"USA" => "United States of America",
"AT" => "Austria"
]

and so on. where anything on the left of the arrow is the key and anything to the right is the value.

Also, your foreach should be

foreach ($arr as $key => $value){
...
}

Upvotes: 4

Related Questions