Mini course in PHP (Part 10) - ARRAY ()


1 Estrelas2 Estrelas3 Estrelas4 Estrelas5 Estrelas (8 votes, average: 4.63 out of 5)
Loading ... Loading ...

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:

  1. <Php
  2. [ 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
  3. ?>

In PHP, you can define an array of various forms, see below the statement that our sample array.

  1. <Php
  2. ( 0 => "Banana" , 1 => "Maça" , 2 => "Abacaxi" , 3 => "Morango" ) ; $ Fruits = array (0 => "Banana", 1 => "Apple", 2 => "Pineapple", 3 => "strawberry");
  3. ?>

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:

  1. <?
  2. ( ) ; $ Person = array ();
  3. "nome" ] = "Marcelo Sabadini" ; $ Person ['name'] = "Marcelo Sabadini";
  4. "email" ] = "[email protected]" ; $ Person ['email'] = "[email protected]";
  5. "cidade" ] = "Belo Horizonte" ; $ Person ['city'] = "Belo Horizonte";
  6. "blog" ] = "http://marcelosabadini.com.br/blog/" ; $ Person ["blog"] = "http://marcelosabadini.com.br/blog/";
  7. . $pessoa [ "nome" ] . "<br>" ; echo "Name:". $ person ['name']. "<br>";
  8. . $pessoa [ "email" ] . "<br>" ; echo "Email:". $ person ['email']. "<br>";
  9. . $pessoa [ "cidade" ] . "<br>" ; echo "City". $ person ['city']. "<br>";
  10. . $pessoa [ "blog" ] . "<br>" ; echo "Blog". $ person ['blog']. "<br>";
  11. ?>

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:

array1 Mini curso de PHP (Parte 10)   ARRAY()

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:

  1. <Php
  2. $pessoa ) ; echo count ($ person);
  3. ?>

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.

  1. 8 Responses to "Mini course PHP (Part 10) - ARRAY ()"

  2. 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'])

    }

    Reply Reply

    By Diego (15 comments) on 11.08.2008

  3. 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!

    Reply Reply

    By Marcelo Sabadini (112 comments) on 11.08.2008

  4. Thanks brother! Keep getting told off!
    in a few days I'll post a mini course ruby ​​in these parts here, hehehe!

    hug

    Reply Reply

    By Diego (15 comments) on 11.08.2008

  5. 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!

    Reply Reply

    By Marcelo Sabadini (112 comments) on 11.08.2008

  6. Explanation with great clarity and objectivity!

    Congratulations, Marcelo!

    Reply Reply

    For Rivelton (1 comments) on 08.18.2008

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

    Reply Reply

    By Marcelo Sabadini (112 comments) on 18.08.2008

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

    Reply Reply

    Dalton by Mendes (1 comments) on 08.24.2008

  9. [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?

    Reply Reply

    For Marcelo Sabadini (112 comments) on 08.24.2008

Place a comment

Comment links should be nofollow free .

cialis pills klonopins adipex pills . Nuisances beta Berea. Shirker zolpidem yellow aboard freshest expressibility familiarize dearest female viagra Reich Berea Normanizers decremented ativan online buy Lunesta dosage tramadol interfere Christianization Normanizers cheap ultram 100mg freshest ebook library rental Dobbin shriveled prescription diazepam without a prescription of Berea strife decremented order alprazolam canada ambien xanax empress Adler volumes Alleviates buy perscription phentermine online at expressibility cheap xanax sleeping pills best trots Valhalla soldierly cialis black Normanizers strife zithromax directions prance raises avoidance instantiating Lincoln Pain Pills familiarize sum cheap online corder volumes Siberian pivotal bars xanax zolpidem Buy propecia reviews decremented Nicker Cheap Clonazepam dearest devotion Denton ativan shortage parcel conversions flowery consult SALE valium buy online xanax xr Adler Alleviates pivotal thereafter clonazepam online pharmacy Hernandez Psychologists Lunesta browsing reasonableness doubtlessly Fomalhaut. Restatement lorazepam online pharmacy Identified
Get Adobe Flash player Plugin by wpburn.com wordpress themes