-2

In this foreach loop:

<?php
$x=array("one","two","three");
foreach ($x as $value)
{
   echo $value . "<br>";
}
?>

How will you be able to access the string "two" individually outside the foreach loop? I don't need to print it, I just need to know how to access it.

4
  • 6
    try to use $x[1] and try to read manual. Commented May 22, 2013 at 7:40
  • Do you want to detect if "two" is in the array? Find its index? What? Commented May 22, 2013 at 7:41
  • Thanks for your response, but what I am trying to find out is how to access the values inside $value. Is that possible? Commented May 22, 2013 at 7:44
  • Jon, I want to know how to access "two". What I did was to push the values from the foreach loop to an array inside a javascript code. My javascript, indeed, stored the values inside the array. However, it just got the first element. So, in effect, what I got inside my array is "one","one","one". Commented May 22, 2013 at 7:50

3 Answers 3

6

Access it by index. The loop is not necessary for that:

echo $x[0]; // one
echo $x[1]; // two
echo $x[2]; // three
3

You can just like this.

$x[1] // two

For further information , Read this under Example #1 A simple array

1

You can access the array direct without the effect of the foreach loop.

$x[1]

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.