Hey guys,
This will be my third try at learning PHP.
This page stumps me a bit:
http://w3schools.com/php/php_arrays.asp
Could you please explain it in terms that are understandable? :S
Thanks![]()

Hey guys,
This will be my third try at learning PHP.
This page stumps me a bit:
http://w3schools.com/php/php_arrays.asp
Could you please explain it in terms that are understandable? :S
Thanks![]()
drink up this bottle of yeah
and P A I N T your body on me
This sorts Peter, Quagmire and Joe into variable/arrays.PHP Code:$names = array("Peter","Quagmire","Joe");
so $names[0] has the value of "Peter"
then.. $names[1] has the value of "Quagmire"
then.. $names[2] has the value of "Joe"
Ah, ok, wut about assertive or w.e?
Also, this http://w3schools.com/php/php_looping.asp confuses me a tiny bit, don't they do the same??
drink up this bottle of yeah
and P A I N T your body on me
Looping is "while()" so you'd use that to loop code, such as getting things from a database.Ah, ok, wut about assertive or w.e?
Also, this http://w3schools.com/php/php_looping.asp confuses me a tiny bit, don't they do the same??
So "name" is a row within your database, and using while will list every row in the table.PHP Code:// other page code, including connecting to your database.
$query = mysql_query("SELECT * FROM `table`");
while($row = mysql_fetch_array($query)) {
echo "This ". $row["name"] ." That.. ";
}
Ok, that sorta confused me :S
This is going to be a long time...
drink up this bottle of yeah
and P A I N T your body on me
while() loops code, but you don't really need to loop code often.
Lets set the stage on fire, and hollywood will be jealous.
Arrays, sexual - I'll explain them how I get them
Think of an array as a single table of data, say a few names like the first post was
Peter
Quagmire
Joe
Put them into a array, separated by commas
By default, unless set, to access 'Peter' would be accessible viaPHP Code:$names = array( 'Peter', 'Quagmire', 'Joe' );
Or instead of that, you could set them yourselfPHP Code:$names[ 0 ];
You could access 'Peter' viaPHP Code:$names[ 514 ] = 'Peter';
$names[ 415 ] = 'Quagmire';
$names[ 666 ] = 'Joe';
You don't have to use numbers as ID keys, for example, you could have the name 'Peter' as a key and have a value behind itPHP Code:$names[ 514 ];
EG:
orPHP Code:$names = array( 'Peter' => 'Fat' );
Multidimensional Arrays are fun tooPHP Code:$names[ 'Peter' ] = 'Fat';
Say, you have different surnames, and behind each surname you got people
Look, you have two surnames in the array 'Smith' and 'Barker', behind each are the names of the people with that surname.PHP Code:$surnames = array(
'Smith' => array( 'Mark', 'John', 'Sally' ),
'Barker' => array( 'Joe', 'Dylan', 'Josh' )
);
Lets access 'Smith'
Now, that will return 'Mark', for JohnPHP Code:$surnames[ 'Smith' ][ 0 ];
Same with 'Barker'PHP Code:$surnames[ 'Smith' ][ 1 ];
Will return 'Josh'PHP Code:$surnames[ 'Barker' ][ 2 ];
Now, you wanted to see how the foreach worked, for example
Just read it as I put it
Say, for each $value in $arrayPHP Code:$array = array( 'Kill', 'People', 'Who', 'Do', 'Not', 'Understand', 'This' );
foreach( $array as $value )
{
echo( $value . '<br />' );
}
So its going to get each value from $array and echo it
the result will be
Kill
People
Who
Do
Not
Understand
This
Last edited by Protege; 23-08-2008 at 09:49 PM.
Hi, names James. I am a web developer.
Post stuckArrays, sexual - I'll explain them how I get them
Think of an array as a single table of data, say a few names like the first post was
Peter
Quagmire
Joe
Put them into a array, separated by commas
By default, unless set, to access 'Peter' would be accessible viaPHP Code:$names = array( 'Peter', 'Quagmire', 'Joe' );
Or instead of that, you could set them yourselfPHP Code:$names[ 0 ];
You could access 'Peter' viaPHP Code:$names[ 514 ] = 'Peter';
$names[ 415 ] = 'Quagmire';
$names[ 666 ] = 'Joe';
You don't have to use numbers as ID keys, for example, you could have the name 'Peter' as a key and have a value behind itPHP Code:$names[ 514 ];
EG:
orPHP Code:$names = array( 'Peter' => 'Fat' );
Multidimensional Arrays are fun tooPHP Code:$names[ 'Peter' ] = 'Fat';
Say, you have different surnames, and behind each surname you got people
Look, you have two surnames in the array 'Smith' and 'Barker', behind each are the names of the people with that surname.PHP Code:$surnames = array(
'Smith' => array( 'Mark', 'John', 'Sally' ),
'Barker' => array( 'Joe', 'Dylan', 'Josh' )
);
Lets access 'Smith'
Now, that will return 'Mark', for JohnPHP Code:$surnames[ 'Smith' ][ 0 ];
Same with 'Barker'PHP Code:$surnames[ 'Smith' ][ 1 ];
Will return 'Josh'PHP Code:$surnames[ 'Barker' ][ 2 ];
Now, you wanted to see how the foreach worked, for example
Just read it as I put it
Say, for each $value in $arrayPHP Code:$array = array( 'Kill', 'People', 'Who', 'Do', 'Not', 'Understand', 'This' );
foreach( $array as $value )
{
echo( $value . '<br />' );
}
So its going to get each value from $array and echo it
the result will be(Forum Moderator) - keep up the unsexual work
Arrays are handy but you don't always need them.
You could use something as simple as:
But stick to arrays as it will be the most professional way of doing things.PHP Code:$name1 = "Bob";
$name2 = "Marley";
echo (' $name1 ');
Want to hide these adverts? Register an account for free!