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

2
  • Reading the question OP wants the first value, not the literal first key. Commented Mar 20, 2020 at 22:02
  • This is probably a more apt dupe, but it's very noisy! stackoverflow.com/questions/1921421/… Commented Mar 20, 2020 at 23:20

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

current($copy) will do if it's a fresh copy.
No, when you copy the array you'll copy the pointer. So reset is a better choice here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.