0

print_r of an array gives me this output:

Array (
    [id-1.txt] => uploads/id-1.jpg
    [id-2.txt] => uploads/id-2.jpg
    [id-3.txt] => uploads/id-3.jpg
)

How can i echo always the first key value ( so uploads/id-1.jpg in this case) without knowing the name of the key? Because the name of the keys are changing everytime...

So: catch always the first value which belongs to the first key, whatever the name of the key is...

john
  • 1,263
  • 5
  • 18
  • Reading the question OP wants the first value, not the literal first key. – Progrock Mar 20 '20 at 22:02
  • This is probably a more apt dupe, but it's very noisy! https://stackoverflow.com/questions/1921421/get-the-first-element-of-an-array – Progrock Mar 20 '20 at 23:20

1 Answers1

1
<?php
$data =
[
    'id-1.txt' => 'uploads/id-1.jpg',
    'id-2.txt' => 'uploads/id-2.jpg',
    'id-3.txt' => 'uploads/id-3.jpg'
];

$copy = $data;

echo reset($copy);
unset($copy);

Output:

uploads/id-1.jpg

Here using a copy for no side effects.

Progrock
  • 7,373
  • 1
  • 19
  • 25