PHP arrays

Discussion in 'Web Design & Programming' started by RHochstenbach, Mar 8, 2011.

  1. RHochstenbach

    RHochstenbach Administrator

    Likes Received:
    Trophy Points:
    From a previous lessen you've learnt about PHP variables. Each variable starts with a dollar sign ( $ ). Each array must have a unique name. But what would you do if you want to assign multiple unique values to one variable? We use something called an Array.

    Let's say we have a fishing club and we want to assign the age of all members to the variable 'members' where every member can be retrieved separately.

    We start off with defining the variable 'members' without a value.
    Now we create an array for each member. We do that using the following syntax:
    $variable['name'] = "value"
    The name between the brackets [ ] should be a unique name in that variable. You can see it as a sub-variable, without the dollar sign. Don't forget to wrap it inside single quotation marks.

    We now create an array for out example:
    $members['john'] = "20";
    $members['frank']= "25";
    $members['sarah'] = "22";
    $members['george'] = "35";
    Each of those is now treated as a unique variable, which can be used in PHP as any other variable. When you retreive data from a database, it gets presented as an array. We'll talk about that in a later lesson.

Share This Page