0

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.

asked Dec 9, 2013 at 0:05

2 Answers 2

3

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 ...
}
answered Dec 9, 2013 at 0:08
1

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
...
answered Dec 9, 2013 at 0:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.