1

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
2
  • 1
    all strings in php can be addressed as arrays $value[0] is the first letter of the string Commented Feb 7, 2013 at 19:05
  • String access and modification by character Commented Feb 7, 2013 at 19:06

5 Answers 5

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

5 Comments

but i can just echo $value outside the loop as $value[0] and still get the first character ?
As long as you have something assigned to the $value variable, yes. Like $value='zxc'; echo $value[0];.
so it mean string variable are arrays?
In a sense, they are arrays of characters
Though you can't, for instance, run foreach over string's characters.
1

You should get

 s m y

on separate lines.

BTW the br tag is old hat.

Comments

1

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',
        //...
    ),
    //...
);

Comments

1

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];
  }

?>

4 Comments

so it mean string variable are arrays?
@AbdulRaziq Check the update, it means even though strings are simple expressions, they can be treated as small object, when accessing them with arrays.
Btw: I think what you are looking for might be the last script.
@AbdulRaziq no problem, I am glad to help. But, if you've found this answer helpfull, make sure to upvote or accept it.

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.