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.

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 ...
}
Sign up to request clarification or add additional context in comments.

Comments

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

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.