Arrays In Php - My Fourth PHP Tutorial

free web hosting
Free Web Hosting, No Ads > CONTRIBUTE > Tutorials

Arrays In Php - My Fourth PHP Tutorial

ghostrider
This is the fourth part of my PHP tutorial. You can view the last three here:

Third Tutorial (Form Data and If statements)
Second Tutorial (Variables)
First Tutorial (What is PHP?)

Intro To PHP
Tutorial 4 - Arrays
Released 4/13/07
By Chris Feilbach aka GhostRider

Contact Info:
E-mail: assembler7@gmail.com
AIM: emptybinder78
Yahoo: drunkonmarshmellows
Website: http://www.ghostrider.trap17.com

Hello once again! During this tutorial I'm going to teach you about arrays. This tutorial does not have a project with it.

Remember back to my second tutorial, when you learned about variables. Variables allow programmers to store data, which then allows to do stuff with it. However variables can only store one value at a time. Arrays however, allow you to store more than one value at a time in the same variable, which makes arrays execeptionally useful in any langauge.

PHP has exactly 75 functions and statements that deal with arrays. They either sort them in different ways, tell you how many elements (values) are in them, or do a bunch of other stuff. We'll use maybe 3 or 4 of them in this tutorial.

Remember back to the last lesson when we learned about form data. The $_GET variable and the $_POST variable are arrays. They are called associative arrays because they have a key that is NOT a number. Arrays work very similar to form data in the sense that they two are in the (name=value) pair, meaning that each value has a name that allows you to access.

An element is a value stored in an array. Lets say we have the following web address:

http://www.ghostrider.trap17.com/index.php...s&submode=1

Lets get the GET data from this

CODE
$mode = $_GET['mode']; // $mode = tutorial
$submode = $_GET['submode']; // $submode = 1


Our keyes are mode and submode. In associative arrays they need to be put around single quotes. If you don't put single quotes around it you get an annoying message from PHP saying that you need to.

The second type of array is called a numerical array. In this case the keyes are integers, starting on whatever integer you want. Any floating number (a number with a decimal after it) will be truncated if you try to use it. Do yourself a favor and always use whole numbers. Unlike associative arrays, the key does not need to be put inside of single quotes.

While a numberical array can start on any number, I advise you to start on 0. Programmers start counting from 0, not 1. From now on you will always see me start my numerical arrays with 0. I highly suggest you do the same.

Arrays are created in one of two ways. You can simply define them like so:

CODE
$ourfirstarray['temp'] = 38593;
$ourfirstarray['name'] = "Chris";
$ourfirstarray['temp1'] = 88.55555;

$oursecondarray[0] = 27;
$oursecondarray[1] = "Hello";
$oursecondarray[2] = $ourfirstarray; // <-- Yes, you can do that in PHP.


Take a look at the last line of code. This now creates something special, something called a multidimensional array. These are very useful, but we won't be using that often yet. They are generally used when code becomes more complex than ours, and has more data to process. Multidimensional arrays can have as many dimensions as you want, but they take up a lot of memory. Use them sparingly and only if you feel you need to.

We can access the data in $ourfirstarray throught $oursecondarray

CODE
$temp = $oursecondarray[2]['temp']; // $temp = 38593
$temp = $oursecondarray[2]['name']; // $temp = Chris
// Be careful however
$temp = $ourfirstarray[2]['temp']; // $temp = NULL (nothing)


Just because one array is multidimensional does not mean the other array is.

There is another way of making an array. You can use the arrange function.

Lets create an array with the keys first and last with the values "Enter your first name." and "Enter your last name."

CODE
$array = array("first" => "Enter your first name.", "last" => "Enter your last name.");

print $array['first']; // Prints Enter your first name.


You can define as many key/value pairs as your wish using the array function. What about multidimensional arrays? You can create those, too.

CODE
$array = array("secondarray" => array("val1" => 22, "val2" => "Data"));

print $array['secondarray']['val1']; // Prints 22


You can also create numerical arrays using the array function. Do not put them in quotes. The array will then be considered associate if you do. Also, if you do not want to specify a new number everytime you define a key, you can specify a number, and then simply continue with printing the values, and not the keys. For example:

CODE
$array = array(0 => 1, 2, 3, 4);

print $array[0]; // Prints 1
print $array[1]; // Prints 2
print $array[2]; // Prints 3


This concludes this tutorial. Next tutorial we'll cover loops, and then the tutorial after that cookies. And after that, we'll start our first major project. Review a lot. You are at the point where you should know enough now to start to develop your own small scripts. Play around with it. If you develop something really cool, send me a link or code.

 

 

 


Reply

Laurie
I appreciate you sharing this course of tutorials. The thing I need that you dont provide is the idea of WHAT to program. Any one with suggestions as to possible concepts, please share them with me. I would like to practice using my programming skills.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : arrays, php, fourth, php

  1. Arrays And List Boxes
    VB6 Codes needed (2)
  2. Arrays Outside A Function
    Need to have arrays available to all functions. (3)
    I've got a bunch of arrays that i want to use for more then 1 function. when i declear the
    arrays outside a function i cant use it in a function. This code was originally written in
    javascript by another person but since I plan to use it and extend it with php I had to change it
    from javascript to php code. In the javascript code the arrays were decleared outside the functions
    with 'var arrayname' I read somewhere that declearing javascript variables with
    'var' gives it global access. Any ideas on how I can go about declearing 1 set of these
    arrays t....
  3. Data Structure -- Arrays -- Odd Number Of Elements
    (0)
    Given an array of elements with many numbers occurring even number of times and two numbers
    occurring odd number of times. Find out the two numbers that occur odd number of times. example:
    Elements in array -- 14433446 The expected result is 1 and 6 One solution 1. Find max of the
    array 2. Hash Function : element/max value 3. Repeat to all elements... 4. Find frequency... yo will
    get the 2 elements with odd frequency. but this is not the optimal.... Do find more solutions to
    this and post it. Look for time and space complexity. Another Solution one more s....
  4. Problem With Global Arrays
    (2)
    I've got a problem with global variables. /sad.gif" style="vertical-align:middle" emoid=":("
    border="0" alt="sad.gif" /> I've read and re-read the stuff about that on php.net and did that
    a few times more, and I'm still not able to make it work. Could somebody please fix my code?
    This is the primary page: include "getLvlFromHigh.func"; $user = $_GET ; $skill =
    $_GET ; $goal = $_GET ; $userXp = getLvlFromHigh($user); $xpTable =
    array( 31 => 14833, 32 => 16456, 33 => 18247, 34 => 20224, 35 => 22406, 36 =>....
  5. [php] Walking Through Multidimensional Arrays
    Walk through multidimensional array with foreach and list (2)
    If you like PHP arrays like i do you probably wondered how to walk through multidimensional arrays.
    You can construct them maybe from your database or flat file, and output them with simple two
    functions foreach, and list.. Let's say we have something like this CODE $information
    = array (                         'id0001' => array ('ivan',
    'ivanovich', 'm', '24'),                         'id0002' =>
    array ('marko', 'markovich', 'm', '21'),             ....
  6. Php - Randomize Web Title
    using arrays and the random function (5)
    PHP - Randomize Web Title with the rand function... Define the variables numbers we start at number
    0 CODE <?php $title[0] = "Web title 0"; // Title 0
    $title[1] = "Web title 1"; // Title 1 $title[2] = "Web title
    2"; // Title 2 $title[3] = "Web title 3"; // Title 3
    $title[4] = "Web title 4"; // Title 4 ?> now we use the rand()
    function to get a randomize the web title. rand(0, 4) means we start at 0 and end at 4 i can get
    0,1,2,3,4 one of those. ....
  7. Basic Tutorial On Arrays
    (3)
    In php u can declare arrays by two methods 1. Using the Array function 2. Using the Array
    Identifier Array Function To define an array consisting of three elements using the array
    function we write the following code CODE $arrayname=
    array("basic","array","tutorial"); This declares an array with
    the name $arrayname containing 3 elements. To access these elements u have to use the array
    name along with the index in square brackets. ** Note: php arrays start from 0 index To print
    the elements of the above array....
  8. C Programming: Arrays
    A Clear Description of Arrays (2)
    C programming provides a capability that enables a user to design a set of similar data types,
    called Arrays. For understanding the arrays properly, let us consider the following program CODE
    main() { int x; j=5 j=10; printf("\nj= %d",x); } No doubt, this
    program will print the value of j as 10. This is because when a value 10 is assigned to j, the
    earlier value of j, i.e. 5, is lost. Thus, ordinary variables are capable of holding only one value
    at a time. However, there are situations in which we would want to store more than one....
  9. What Is The Fourth Dimension?
    something like a hypercube...? (79)
    I recently heard from one of my cousins about the fourth dimension. Have you heard of it so far ??
    Well, to explain it,it is something like a hypercube. A point is a zero dimensional object. If you
    drag this point along the x axis,you get a line a one dimensional object.If you drag this line along
    the y axis you get a square which is a two dimensional object. If you drag this square along the z
    axis you get a cube which is a three dimensional object.And if you drag this cube along the ana and
    kata directions ,bingo !!!! we get a four dimensional object. B....
  10. Converting Characters In A Variable To Individual Values In An Array
    turning variables into arrays (2)
    Say I have a variable such as $nav_item and it had to contents Home . IE: CODE
    $nav_item = 'Home'; How would I make so that $nav_item was an array and
    had the following contents? CODE $nav_item = array ('h', 'o',
    'm', 'e'); With the case changing (ie H would become h and U
    would become u ) EDIT: Okay found out that I could change the case with
    array_change_key_case ($nav_item, CASE_LOWER); ....
  11. Wild Arms 4
    Fourth Detonator (0)
    Wild ARMs 4 has been released in US by Xseed Games. I've played it and so much it's very
    promising. The story is so much different than the previous Wild ARMs. No metal demons or Hiades
    demons. It's now the time Brionac Force to take the trone. It's said the war between
    Congress and (somwheat)Kingdom has ended. The war itself left out the destroyed state of Filgaia,
    with the scars of the war everywhere. However, is it really end? Whenever there is human, there will
    always be the next war... Jude, a normal boy one day met something different in his peac....
  12. Alternative To File()?
    file stored in arrays (4)
    Hey guys. I was wondering if there is an alternative to using file(). I'm trying to read a file
    and store it into an array without using the file function. Before, I was doing something like this:
    CODE <?php  $file = file("http://yahoo.com"); ?> Simple.
    It gets the contents of index.html at yahoo.com and store each line in an array. Well, I was
    wondering if there is another code that could do that without using the file function. Thanks for
    helping!....
  13. Appending Arrays
    (4)
    When you have a normal variable say $string and you want to add more to it you would use the .=
    construct, but how does one go about doing that with an array, I have an IF structure that determins
    what options the user has selected, so I have something like: CODE $array = NULL; IF
    (isset($_POST['op1])){ $array .= array(1, 2, 3, 4); }
    IF (isset($_POST['op2'])){ $array .= array(5, 6, 7,
    8); } So if there is a value for op1 then the array contains 1,2,3,4 is th....
  14. 3d Arrays [resolved]
    (0)
    I'm making an oct tree to divide up a colour space. Each tree has 3 upperbounds, and 3
    lowerbounds, 1 pair for each axis. I store it's children in a 3D array. 2 by 2 by 2.
    m_pChildren is of type pointer to a pointer, and is initilized like this: Code: void
    COctTree::CreateChildren(void) { m_pChildren = new COctTree* ; for(int i=0;i {
    for(int j=0;j { for(int k=0;k { m_pChildren = new COctTree(); //Setup
    boundaries m_pChildren ->m_pLowBounds = m_pLowBounds + i * ((m_pHighBounds -
    m_pLowBounds )>>1); ....

    1. Looking for arrays, php, fourth, php

Searching Video's for arrays, php, fourth, php
advertisement



Arrays In Php - My Fourth PHP Tutorial



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE