Abdul Raziq
Abdul Raziq

Reputation: 101

PHP foreach loop confusion?

So a simple example would be

$ar = array("some text","more text","yet more text");

foreach($ar as $value){

echo $value."<br>";

}

I get the result

some text
more text
yet more text

so my question is when we do this inside foreach loop "$ar as $value", I know that $ar is array but what about second one the $value is it simple variable or is it yet another array? Because we can do it in the following way too

foreach($ar as $value){

echo $value[0]."<br>";

}

Which would result in

s

Upvotes: 1

Views: 150

Answers (5)

samayo
samayo

Reputation: 16495

String access and modification by character is possible in PHP. What you need to know, and probably didn't know is that while strings are expresses as string, sometimes they can be considered as arrays: let's look at this example:

$text = "The quick brown fox...";

Now, if you were to echo $text[0] you would get the first letter in the string which in this case happens to be T, or if you wanted to modify it, doing $text[0] = "A"; then you will be changing the letter "T" to "A"

Here is a good tutorial from the PHP Manual, It shows you how strings can be accessed/modified by treating them as an array.

<?php
// Get the first character of a string
$str = 'This is a test.';
$first = $str[0];

// Get the third character of a string
$third = $str[2];

// Get the last character of a string.
$str = 'This is still a test.';
$last = $str[strlen($str)-1]; 

// Modify the last character of a string
$str = 'Look at the sea';
$str[strlen($str)-1] = 'e';

?>

BTW: If you had only wanted to display, the first value inside your array, you could use something like

<?php
$ar = array("some text","more text","yet more text");

for ($i=1; $i<=1; $i++)
  {
  echo $ar[0];
  }

?>

Upvotes: 1

Hugo Dozois
Hugo Dozois

Reputation: 8420

The thing is that doing $value[0] access the first character of the string.

A string is internally represented as an array. So accessing to the index 0 of a string is like accessing to the first character.

That is why it prints "s" because your string "some text" starts with s

You can see your example as the following

array(
    [0] => array(
        [0] => 's',
        [1] => 'o',
        [2] => 'm',
        [3] => 'e',
        //...
    ),
    [1] => array(
        [0] => 'm',
        [1] => 'o',
        [2] => 'r',
        [3] => 'e',
        //...
    ),
    //...
);

Upvotes: 1

Ed Heal
Ed Heal

Reputation: 59987

You should get

 s m y

on separate lines.

BTW the br tag is old hat.

Upvotes: 1

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143051

$value is a value in array and is not an array itself unless you have nested arrays (array(array('a','b'),array('b','c'))). Subscripting strings, though, is still possible and this is how your got the first character of the string.

Upvotes: 1

Kermit
Kermit

Reputation: 34055

In PHP, strings are byte arrays. Referencing position 0 of $value refers to the position (0) in the string (s in some test)

Your actual array looks like this:

Array
(
    [0] => some text
    [1] => more text
    [2] => yet more text
)

If you want to access the index position of the array you can do:

foreach($ar as $key=>$val) {
    echo "$key - $val";
}

Which will output:

0 - some text
1 - more text
2 - yet more text

Upvotes: 1

Related Questions