PHP Mini Course
- 15/07/2008
Mini course in PHP (Part 10) - ARRAY ()
http://br.php.net/manual/pt_BR/book.array.php
Hello, Today in class we'll look at array (). Array is a type of variable, remember the lesson when we saw two types of is a type of variable position is, or is a variable that can hold more than one value. For example, you can have an array that stores color, or an array that holds day of the week, or an array that stores the data of a person. So would a variable called $ person that contain the information: name, address, e-mail, phone, etc ...
Not to complicate matters, I'll show an example of array of fruit ok?
$ Fruits = array ("Banana", "Apples", "Pineapple", "Strawberry");
Got it? The variable $ fruit contains multiple values.
An array is sorted by indices, ie, to access the first position of the array $ fruit, we must inform the index: $ fruits [0].
By default, an array ALWAYS starts at index 0 (zero).
So if I want to print on the screen at the second position of the array, I do it:
- <Php
- [ 1 ] ; // a segunda posição tem q ser 1 pois o array começa com 0 echo $ fruits [1] / / the second position must be a q because the array starts with 0
- ?>
In PHP, you can define an array of various forms, see below the statement that our sample array.
- <Php
- ( 0 => "Banana" , 1 => "Maça" , 2 => "Abacaxi" , 3 => "Morango" ) ; $ Fruits = array (0 => "Banana", 1 => "Apple", 2 => "Pineapple", 3 => "strawberry");
- ?>
In the example above, see I'm explaining the array index, I could use one word instead of numbers ok? EX: $ fruits = array ("first" => "Banana", "second" => "Apple");
Another way is to declare the array:
$ Fruits [] = "Banana"
$ Fruits [] = "Apple"
$ Fruits [] = "Pineapple"
$ Fruits [] = "Strawberry"
Doing this way, each line PHP places the value at the end of the array, as it were to do so:
$ Fruits [0] = "Banana"
$ Fruits [1] = "Apple"
$ Fruits [2] = "Pineapple"
$ Fruits [3] = "Strawberry"
Ok, now let's make another array by placing some data ... Let's make an array with data from a person. I'll do it in the editor to comment on the power lines ... See image below:
- <?
- "nome" ] = "Marcelo Sabadini" ; $ Person ['name'] = "Marcelo Sabadini";
- "cidade" ] = "Belo Horizonte" ; $ Person ['city'] = "Belo Horizonte";
- "blog" ] = "http://marcelosabadini.com.br/blog/" ; $ Person ["blog"] = "http://marcelosabadini.com.br/blog/";
- ?>
Explanation of code:
Line 2: Statement of the variable is an array. This is to ensure that no errors occur when running the script, because on some servers there is a directive in the configuration of PHP that this array would not declare an alert appears on the screen .. but the script would run.
Line 3: We create a position in the array called 'name'.
Line 4: We created a position in the array called 'email'.
Line 5: We build a position in the array called 'city'.
Line 6: We created a position in the array called 'blog'.
Line 8 to 11 have it print on the screen the values that were stored in the array.
But the reason to learn to work with arrays?
Simple, imagine that this is a page with details of registered users in the database ... where we declare the array $ person would be the part where we would query the database, which would return an array with the data of the selected user. Regardless of the person selected would not change anything until the line 8 to line 11. For each index, would be filled with data from the database.
Running this script, we get the following output:
If you look at the reference spent it at the beginning of the post, you will see many functions for working with array. I'll show you some very useful functions.
count (): You pass an array as a parameter and it returns how many indexes this array has (its size). Ex:
sort () / rsort (): Sort the array
sort ($ person) / / sorts an array in ascending order
rsort ($ person) / / sorts an array in increasing order DE
I passed the link, has all the functions available to work with the array. Come and take a look.
Why's it! Hope this helps .. any questions please leave a comment.




8 Responses to "Mini course PHP (Part 10) - ARRAY ()"
Marcelão, here's expensive!
Cock the course! continue with such clarity and objectivity
hug!
Fix it here O:
$ Knowledge = count ($ person ['knowledge']);
for (i = 0; $ i <= $ knowledge; {$ i + +
echo ($ person ['knowledge'], ['$ i'])
}
By Diego (15 comments) on 11.08.2008
Great Diego ..
Almost kra ...
You forgot to close o) is: for (...)
He put the; in the wrong place at the time of printing knowledge. You put it like this: echo ($ person ['knowledge'], ['$ i']), and certain is this: echo ($ person ['knowledge'] ['$ i']);
Another thing, there is no need to put the contents of the parentheses echo ok?
The correct code looks like this:
$ Knowledge = count ($ person ['knowledge']);
for (i = 0; $ i <= $ knowledge; $ i + +) {
echo ($ person ['knowledge'] ['$ i']);
}
Kra big hug .. qq only post something!
By Marcelo Sabadini (112 comments) on 11.08.2008
Thanks brother! Keep getting told off!
in a few days I'll post a mini course ruby in these parts here, hehehe!
hug
By Diego (15 comments) on 11.08.2008
Well, now for those who did not understand I will explain.
Reasoning:
1) We need to know how much knowledge one has (array size);
2) We need to show all the knowledge of the array on the screen;
- The number 1 already know how, I showed that there is a count () function that returns the size of the array right?
- The size of the array at hand (in one variable (called the Diego $ knowledge, he did it: knowledge = $ count ($ person ['knowledge']) ;) let's do a FOR (seen in class 6) the number of turns is equal to the size of the array.
for ($ i = 0; $ i <= $ knowledge; $ i + +) {
}
and within the IS we print each íncices the array, as you saw in class, to print an index you put in [] right? type: $ array [1].
So we have printed:
$ Person ['knowledge'] [$ i]
YES, put the index of the array $ person ['knowledge'] is the return of the IS, because every back $ i is incremented, começanco with 0 (zero).
Imagine that it will print the following:
$ Person ['knowledge'] [0]
$ Person ['knowledge'] [1]
$ Person ['knowledge'] [2]
and will print up to finish the array.
If you do not understand, please post here or send me an email ok?
hugs!
By Marcelo Sabadini (112 comments) on 11.08.2008
Explanation with great clarity and objectivity!
Congratulations, Marcelo!
For Rivelton (1 comments) on 08.18.2008
Rivelton Hello, I'm glad you liked the class q ...
My intention is just that ... be clear and objective ... without the rodeos that handouts are always hehehe
a big hug!
By Marcelo Sabadini (112 comments) on 18.08.2008
Hi Marcelo, okay? Is not understood how the sql to insert more than one aquivo the same field of a table. Or do not you?
To picking and much hehehe
thank you
Dalton by Mendes (1 comments) on 08.24.2008
[Dalton]
- Kra Sorry but I could not understand the 'insert sql for more than a aquivo in the same field in a table.' Would explain that again?
For Marcelo Sabadini (112 comments) on 08.24.2008