I have this array:
$predmeti = [
'slo' => 'slovenščina',
'mat' => 'matematika',
'ang' => 'angleščina',
'fot' => 'fotografija',
'tir' => 'tipografija in reprodukcija',
'tirv' => 'tipografija in reprodukcija vaje',
'gob' => 'grafično oblikovanje',
'mob' => 'medijsko oblikovanje',
];
Somewhere in code I want to get all first values of this array (slo, mat, ang...) How do I achive that?
I need to pass all the values in foreach statment after getting all first values.
2 Answers 2
What you call the "first value" is the "key". You can use array_keys
to get an array of all the keys from the first array:
$keys = array_keys($predmeti);
foreach ($keys as $key) {
// ... do something with the $key ...
}
But since you're using a foreach loop anyway, you can iterate the keys and values of the array together, and just ignore the $value
variable:
foreach ($predmeti as $key => $value) {
// ... do something with the $key ...
}
The "first values" you are talking about are the keys, the "second values" are the actual values. Try:
foreach ($predmeti as $key => $value) {
print "key: $key, value: $value\n";
}
That should print
key: slo, value: slovenščina
key: mat, value: matematika
...