[prev in list] [next in list] [prev in thread] [next in thread]
List: php-general
Subject: =?utf-8?B?UmU6IFtQSFBdIEFycmF5IHF1ZXN0aW9u?=
From: "=?utf-8?B?YXNoQGFzaGxleXNoZXJpZGFuLmNvLnVr?=" <ash () ashleysheridan ! co ! uk>
Date: 2010-09-26 10:03:33
Message-ID: 99.80.31107.A7A1F9C4 () pb1 ! pair ! com
[Download RAW message or body]
[Attachment #2 (text/plain)]
I'd also like to add to that:
$array = array();
$array[] = 'text';
$array[2] = 123;
$array[] = 'hello';
Would output:
$array(
0 => 'text',
2 => 123,
3 => 'hello',
);
Note the missing index 1, as php makes a numerical index that is one greater than the \
highest already in use. As the index 2 was explicitly created, php made the next one \
at 3.
Thanks,
Ash
http://www.ashleysheridan.co.uk
----- Reply message -----
From: "chris h" <chris404@gmail.com>
Date: Sat, Sep 25, 2010 22:05
Subject: [PHP] Array question
To: "MikeB" <mpbrede@gmail.com>
Cc: <php-general@lists.php.net>
Mike,
$results[] will automatically push a value unto the end of an array.
So doing this...
------
$magic = array();
$magic[] = 'a';
$magic[] = 'b';
$magic[] = 'c';
-----
is exactly this same as doing this...
------
$normal = array();
$normal[0] = 'a';
$normal[1] = 'b';
$normal[2] = 'c';
-----
And yes, in your example "$results[]" would be equivalent to "$results[$j]"
For more reference:
http://www.php.net/manual/en/language.types.array.php
Chris H.
On Sat, Sep 25, 2010 at 4:31 PM, MikeB <mpbrede@gmail.com> wrote:
> I have the following code:
>
> $query = "SELECT * FROM classics";
> $result = mysql_query($query);
>
> if (!$result) die ("Database access failed: " . mysql_error());
> $rows = mysql_num_rows($result);
>
> for ($j = 0 ; $j < $rows ; ++$j)
> {
> $results[] = mysql_fetch_array($result);
> }
>
> mysql_close($db_server);
>
> My question, in the loop, why does tha author use:
>
> $results[] = mysql_fetch_array($result);
>
> instead of (as I would expect):
>
> $results[$j] = mysql_fetch_array($result);?
>
> What PHP magic is at work here?
>
> Thanks.
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic