Topic: "php arrays..." (page 1 of 1)

1
Author Post
sniperkid
groupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmaster
ok im not too confident in arrays when i used them in perl, but anyways in php i have it write some stuff to files like for example;

test
testpass
18
uk

and i want to read them, how would i put it in a array and split them up??
$array1 = @array[0]; or something like that?

Thanks
private message Website
qwertydawom
groupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmaster
Well, I could tell you wgat you want, but, a friend of mine just made a tut' about the php arrays ! :) (his nick is WhiteAcid) :
QuoteQuote from WhiteAcid:
PHP-Arrays

Abstract
In this tutorial you will learn what arrays are and how to use them. You’ll get the hang of multidimensional arrays and where they can be used effectively. To fully grasp this article you’ll obviously need to know some PHP. You’ll need to know what variables are, what loops are and how they work.

1.1 – What they are?
Firstly I’ll talk about what an array is, concisely, it’s a group of variables, stored in a list. Imagine you have a website that talks about cars; I’ll be using this example quite a bit throughout this tutorial. This site has one page which lists all the cars mentioned on the site. Now, you could have something like:

Code:
car1 = "henry";
car2 = "regina";
car3 = "ol’ bob";

Then later you could echo out each variable on its own. This code has a few da disadvantage that it requires editing in two places when you want to add/remove a car (the list of variables, and the echo place).

Arrays are a perfect solution for this. Read on to find out how.

1.2 – How to declare and initialise them?
Whoa! That title might have blown you away a bit. Declaring something is to simply start it off or to set it up, to initialise something (in terms of programming) is to give something a value.

Let’s continue to car example, we want to make an array which can hold the names of all the cars mentioned on our site.
Let’s first initialise our array. There are a few ways to do this in PHP, I’ll use the one I use. I use it because it’s similar to JavaScript which I knew before I learned PHP, more importantly I find it easy for others to read the code if you use this method.

Code:
$cars = array()
That’s all that’s needed to create an array. We now have our array called cars, which is at the moment an empty array.

We now need to give values to the arrays elements. An array can have an number of elements infinite (or until the computers memory runs out), an element being one value held in the array. Each element has an index, which is used to read and write to that element. It’s a standard to have the indexes go up 1 and start at 0, this also simplifies looping through array, which we’ll do later. Here’s how to fill in three elements in the array:

Code:
$cars[0] = "henry";
$cars[1] = "regina";
$cars[2] = "ol’ bob";

See how, after the name of the array, I have written a number inside square brackets? Those square brackets are what contains the index, this of course means the integers are the index of the elements. The nought element (as I started at 0), is now equal to “henry”. To access these elements you use it’s index, to echo out “regina” I’d write:

Code:
echo $cars[1];

To write them all out, without thinking a bit, we’d still have to echo every element out on its own, so we’ve not really saved any time, in fact you’ve wasted time reading this tutorial. But hang on a sec and you’ll see how the amount of code can be greatly reduced, at least if you have 20+ cars, instead of our pitiful 3.

Since our index value starts at 0 and goes up by one, this is perfect for a loop. Before this though, we need to know about a very simple function, native to PHP. This is the count() function. It takes an array as its parameter and returns the number of elements within that array. This is essential, as we’d have no other way of knowing how many times to loop. We’ll use a for loop, now, let’s think about this. We’ll start at i=0 (the lowest index of the array) and we’ll keep going until i=number_of_elements_in_array. Ok, after putting on our brain cap, we’ll end up with

Code:
for ($i=0; $i<count($cars); ++$i)
{
echo $cars[$i]."<br />";
}
Now then, the first line makes our loop the correct amount of times, the line with echo on it spits out the ith element of our array. Get it? Think about it, it should be fairly much common sense.

Now then, do you see how this could have saved 100s of lines of code if we had several cars? And it could save plenty of coding time too.

It should also be noted that elements in these arrays can still be treated as strings, numbers or whatever they are. We could reverse each element in our array by looping through it and doing $cars[$i]=strrev($cars[$i]);.

1.3 - Multi-dimensional arrays
If we did have a site talking about cars, we wouldn’t just want to be able to list their names, we’d want their year of production, retailer, and how much money has been stuffed down between the seats. Some of you might be thinking that this can’t be done, back to single variables for you eh? Some smarter people might be thinking to have several one-dimensional parallel arrays (many arrays, where the index of one, also refers to the element with the same index from another array). Most of you will have read the title and realised that multi-dimensional arrays are the way to go, whatever the hell they are.

A multi-D array is an array where each element is also an array. This may sound confusing, but it’ll all become clear eventually. Let’s make our main array called cars. We’d define that as just before. We then want each of its elements to also be an array, holding (in this order) name, year of production, change found in seat. Here’s the code:

Code:
$cars = array(); //same as before
//$cars = array(name,year of production, change found in seats);
$cars[0] = array("henry","1985","5.88");
$cars[1] = array("regina","1975","3.24");
$cars[2] = array("ol’ bob","1971","0.12");See how each element of cars is an array of its own? Now we come to the problem of accessing each element, to read and write from or to it. We know that cars[1] will access the element referring to the car called regina, but how do we access the name regina itself? Well, cars[1] is an array itself, so it contains elements too, “regina” is the nought element, so you can access it by cars[1][0].

Looping through these arrays is still extremely simple. The first of the two index numbers is still the only one we need to edit to change from one car to another (changing the second index number would get you from one property of that car to another). Here’s some code to loop through the cars and echoing out some info about it:

Code:
for ($i=0; $i<count($cars); ++$i)
{
echo "name: ".$cars[$i][0]."<br />";
echo "year: ".$cars[$i][1]."<br />";
echo "money off: £".$cars[$i][2]."<br />";
}

I hope this helps.. if not, feel free to ask ;)
private message Website

Topic: "php arrays..." (page 1 of 1)

1