0

I have an array that contains approx 13 items.

What I'm looking to do, is to find the 10th item in the array and display it.

The output from var_dump is:

array(13) { [0]=> int(0) [1]=> int(1) [2]=> int(2)  etc etc 

Ideally, I'd like to check if the 10th element exists first and if it does, display, it.

Many thanks

1
  • Do you want the element with index 9 (or 10 if you get the question wrong), or the 10th element of array? An array like so: $arr = Array([1]=>1,[5]=>1,[9]=>1,[10]=>1...) have $arr[9] != to effective 10th element Commented Mar 21, 2012 at 10:23

5 Answers 5

1

if(array_key_exists(10, $arr)) {
  echo $arr[10];
}
Sign up to request clarification or add additional context in comments.

Comments

1
if(isset($array[9])) echo $array[9];

Comments

1
echo isset($array[9]) ? $array[9] : null;

that should work

or

if(isset($array[9]))
    echo $array[9];

Comments

1

if(isset($array[9])) { echo $array[9]; }

Comments

0
if (isset($array[9])) echo $array[9];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.