Terug naar de voorpaginaTBForum nu ook op je mobiel!
Klik hier voor meer info en gratis link...

Home Nieuw Vraag & Aanbod Forums Artikelen Bedrijvengids Zoeken

   Richard v D., FireMultimedia
27 okt 2008 21:25 
Introduction
This is my first article in English. I have chosen for an article about array's in PHP because it makes the life of a php scripter easier.

Arrays are very useful in combination with mysql queries. Before starting with this tutorial please learn the basic about PHP like echo, variables and strings.

What is an Array?
An Array is a Variable with more than one string.

A variable have one string, see the example code:

$usernameta = 'edward';
$usernameb = 'john';

$agea = '20';
$ageb = '34';

echo $usernamea.'age is '.$agea.'<br />';
echo $usernameb.'age is '.$ageb.'<br />';


The same result with two arrays, one for the age and one for the username:

$username[0] = 'edward';
$username[1] = 'john';

$age[0] = '20';
$age[1] = '34';

echo $username[0].'age is '.$age[0].'<br />';
echo $username[1].'age is '.$age[1].'<br />';


The first number for a array is 0, unaltered as all other numbering in php.


The power of arrays
In combination with other functions from php you can make an automatic loop, this is not possible with normal variables. I show an example

with for

<?php
// how many users?
$show = 3;

$username[0] = 'edward';
$username[1] = 'john';
$username[2] = 'eddy';
$username[3] = 'Richard';

//I have leave the numbers, php know that automatic.
$age[] = '20';
$age[] = '34';
$age[] = '13';
$age[] = '19';



echo 'The first three users:<br />';

for ($count = 0; $count < $show; $count++)
{
echo $username[$count].' age is '.$age[$count].'<br />';
}
?>

The output is:

The first three users:
edward age is 20
john age is 34
eddy age is 13




An another method for filling an array with the same result is:

$username = array('edward','john','eddy','richard');


Not only a rising numbering is possible, you can use also give a name. For example:

$connect['server'] = 'localhost';
$connect['database'] = 'databasename';
$connect['user'] = 'root';
$connect['password'] = 'dontshowme';


in_array
There are a lot of functions for using with arrays, I show one function: in_array. With in_array you check of something present is in the array.


<?php
$username[0] = 'edward';
$username[1] = 'john';
$username[2] = 'eddy';
$username[3] = 'Richard';

$me = 'Richard';

if (in_array($me,$username)){
echo'Richard is present!';
}

?>


Mistakes in this article about my English please sent a private message . Please feel free to add your questions and comments.

     Aangepast op 28-10-2008 17:08 door moderator: spelling
   Mark V, Particulier
28 okt 2008 13:26 
for ($count = 0; $count <= $show-1; $count++) is the same as:
for ($count = 0; $count < $show; $count++).
This version is faster (not much but it is faster) because it doesnt has to calculate the $show - 1 at every loop. Besides that, I think it looks nicer.


$connect[server] = 'localhost';
$connect[database] = 'databasename';
$connect[user] = 'root';
$connect[password] = 'dontshowme';


server,database,user,password aren't constants. They are strings, so they should be used within '' or "":

$connect['server'] = 'localhost';
$connect['database'] = 'databasename';
$connect['user'] = 'root';
$connect['password'] = 'dontshowme';



All the PHP array functions: (Log in om link te zien!)

     Aangepast op 28-10-2008 13:27 door Mark V
   Patrick DG, Particulier
28 okt 2008 15:26 
I don't like the way you use two different arrays for information about the users. Besides that, age should be an integer, so there's no need for ''.

<?php

$user[0]->name = 'edward';
$user[0]->age = 20;
$user[1]->name = 'john';
$user[1]->age = 34;
$user[2]->name = 'eddy';
$user[2]->age = 13;
$user[3]->name = 'Richard';
$user[3]->age = 19;

?>

   Christian S., Particulier
28 okt 2008 15:42 

<?php

$user['name'][0] = 'edward';
$user['age'][0] = 20;
$user['name'][1] = 'john';
$user['age'][1] = 34;
$user['name'][2] = 'eddy';
$user['age'][2] = 13;
$user['name'][3] = 'Richard';
$user['age'][3] = 19;

?>




   Patrick DG, Particulier
28 okt 2008 16:33 
That's not very logical.

The age and name are assigned to the user. That isn't the case in your code.

<?php

$user[0]->name = 'edward';
$user[0]->age = 20;
$user[1]->name = 'john';
$user[1]->age = 34;
$user[2]->name = 'eddy';
$user[2]->age = 13;
$user[3]->name = 'Richard';
$user[3]->age = 19;

foreach($user AS $key => $value){
echo $key.": ".$value->name." - ".$value->age."<br>";
}

?>


Or imagin this

<?

$user[0]->name="henk";
$user[0]->age=13;
$user[1]->name="jaap";
$user[1]->age=25;
$user[2]->name="jan";
$user[2]->age=15;

$user[0]->friends[0]=$user[1];
$user[0]->friends[1]=$user[2];


print_r($user);

?>




returns:

Array
(
....[0] => stdClass Object
........(
............[name] => henk
............[age] => 13
............[friends] => Array
................(
....................[0] => stdClass Object
........................(
............................[name] => jaap
............................[age] => 25
........................)

....................[1] => stdClass Object
........................(
............................[name] => jan
............................[age] => 15
........................)

................)

........)

....[1] => stdClass Object
........(
............[name] => jaap
............[age] => 25
........)

....[2] => stdClass Object
........(
............[name] => jan
............[age] => 15
........)

)





Replaced the spaces with .'s

     Aangepast op 28-10-2008 16:38 door Patrick DG
   L.R. Vries, Border-IT
28 okt 2008 16:40 
Patrick,

You are using a object in a array, and it isn't necessary

$person[]= array('Name' => 'Jaap', 'Age' => 20);


Thats the best way

     Aangepast op 28-10-2008 16:58 door moderator: bad language
   Richard v D., FireMultimedia
28 okt 2008 17:06 
Thanks for the comments.

Moderators: Please edit this article with the comments from Mark de Voogd (first comment) for a correct article.

   Patrick DG, Particulier
28 okt 2008 17:21 
Tell how how that's better than using objects.

It's all about the structure.
With objects, you'll get an array of users.

Not to say your way doesn't work. It'll just get pretty ugly if your data is more complex. Besides that, I don't like to use integers/strings etc all in one array. Objects are made for that purpose.

   Dennis Verjans, Particulier
3 nov 2008 03:52 
// how many users?
$show = 3;
Why don't you use sizeof?
(Log in om link te zien!)

   Richard v D., FireMultimedia
3 nov 2008 12:08 
The array have 4 users and i want to show 3 users. Sizeof is very useful to show the whole array.




 
© Copyright TargetMedia 2001-2012 | Mobile | Premium SMS | Micropayments | Muziek downloaden | Ringtones Bekijk bezoekers statistieken RSS feed