PDA

View Full Version : [Tutorial] [PHP] Arrays



triston220
04-12-2011, 08:58 PM
This tutorial will hopefully explain to you what arrays are, and how they are used.

An array is useful for when you want to store multiple values without having to define multiple variables.

Defining the Array

Defining the Array is simple enough:


$array = array("1", "2", "My_Key" => "My Value"); //Defining our array.

The first two values in our array have no key associated with. This means that we can't access these values unless we know there position within the array. An array with at least one value that doesn't have a key linked to it has a starting index of 0. Accessing values is very easy:


Echo $array[0];

The above will output '1'. To the page.

As expected, to get the value '2', the following code can be applied:



Echo $array[1];

However, accessing a value in an array with a linked key is slightly different. We use the key's name:


Echo $array['My_Key'];

Just as you might imagine, this will output "My Value".

The last way I'm going to cover for getting the values for an array is to loop through it.


foreach ($array as $part){
Echo $part . "<br>";
}

This will Echo

"1
2
My Value".

Adding Values to Arrays

To add a value to an already existing array is also pretty simple.

If you don't want to link a key to it, empty square braces are required.



$array[] = "New Value";

If you do want to link a key, these braces will need to be filled.



$array['Key_Name'] = 'My New Value';

Removing Arrays

Now that we've covered adding values to an array, lets go through how to remove an array.

To remove the array completely, simply call the 'unset()' function on the array:


unset($array);

If, however, you only want to remove one element in the array, you will need to know it's index location or it's key name.


unset($array['My_Key']);

After unsetting an element from the array, always call the array_values() function.


$array = array_values($array);

This will reset the index of the array.

I hope this tutorial is useful. :)

Thread moved by Jordan (Forum Super Moderator): From 'Designing & Development' to here as more suited

Dentafrice
05-12-2011, 01:54 PM
This just proves that you have no idea what you're talking about.


After unsetting an element from the array, always call the array_values() function.

This is some of the most stupid advice, from a PHP beginner.

The numerous downfalls of this are noticeable by any proficient PHP developer.

You specified always, and put it in bold. This means that no matter what the array is, you should always call array values.

In your tutorial you explained about arrays having keys/indexes. Any time that you call array_values, your indexes/keys are then lost for good andre-indexedback to numerical form. This is stupid and will lose the integrity of your data. That is the purpose of the keys.

It's especially important if you have a multidimensional array. Calling array_values will shift it and, if it had a key, reset it to something else.

I don't really see why you would want to re-index every single time you removed an element from the stack. All it does is shift it up. If you have the indexes: 0, 1, 2, 3, 4, 5, and you remove 3. When you add a new one, it will still add 6 as the newest one.

When you iterate through them, it's not going to matter. The only time it's going to matter is if you used a for loop with a counter and tried to do that. Either way, you couldn't do that with non numerical keys anyway.

----

When someone codes an application and uses an array, they most likely know the location of the data in the array if it is hand coded. Why would you want to take the location of $config["server"], $config["username"], and then temporarily add $config["temp"]... use it.. then unset($config["temp"]) it.. when you "ALWAYS" call array_values, your whole configuration file is upset.

This makes no sense and just proves that you don't have any logic whatsoever in you.

Also... why the heck are you capitalizing echo? It is a language construct and just looks stupid capitalized and very cluttered. Again, a very newbie mistake.

----

I mean don't get me wrong, the tutorial is a nice idea and a good try, but you just don't seem to know what you are talking about.

triston220
05-12-2011, 02:57 PM
No, you're just arrogant. The reason I said to always do it is if they are looping using an integer, however I forgot to add that method to my tutorial, causing you to be a bit baffled.

And as for the fact that I capitalise Echo, that's a preference. I personally don't like it's when it's lowercase. Not a mistake, a choice.

Why do you think I'm a PHP beginner? My skill-set seems good enough for me to produce projects which are purchased. If you're that much of a PHP God, why don't you offer some examples, instead of slating everything anybody else says?

I can just about realise that you are trying to help me, but you are coming across more as someone just likes feeling better than others.

Zak
20-12-2011, 01:11 PM
Hey, I've never done arrays in PHP before, only in Java. But when adding to arrays couldn't you simply add on to what you put in the first place?

example:$array=array("1","2","3","4","5");
?

Can you mix arrays as well? I'm still really a beginner in Java but I know you have to specify the array type (double,int,String) etc. You don't have to do this in PHP? :) Thank you!

triston220
20-12-2011, 02:41 PM
Hey, I've never done arrays in PHP before, only in Java. But when adding to arrays couldn't you simply add on to what you put in the first place?

example:$array=array("1","2","3","4","5");
?

Can you mix arrays as well? I'm still really a beginner in Java but I know you have to specify the array type (double,int,String) etc. You don't have to do this in PHP? :) Thank you!

I'm not sure what you mean by your first point. As for mixing arrays, there is a function called array_merge which allows you to 'combine' arrays together.

Zak
20-12-2011, 03:32 PM
In the part where you're adding a value to an already existing array

Your example:$array[]="NewValue"

couldn't you have just done

Your example: $array=array("1","2","My_Key"=>"MyValue");//Definingourarray.
My example for adding to the existing array: $array=array("1","2","My_Key"=>"MyValue","4","5","etc");

??

For your example looks more complicated than if you can do it that way

Your example again:$array[]="NewValue"

What index in the array would it put that new value? I'm guessing at the end? with my example if you could do it that what you would know which index the value would be?

Sorry if this is confusing you :P

triston220
20-12-2011, 03:37 PM
In the part where you're adding a value to an already existing array

Your example:$array[]="NewValue"

couldn't you have just done

Your example: $array=array("1","2","My_Key"=>"MyValue");//Definingourarray.
My example for adding to the existing array: $array=array("1","2","My_Key"=>"MyValue","4","5","etc");

??

For your example looks more complicated than if you can do it that way

Your example again:$array[]="NewValue"

What index in the array would it put that new value? I'm guessing at the end? with my example if you could do it that what you would know which index the value would be?

Sorry if this is confusing you :P





$array=array("1","2","My_Key"=>"MyValue");//Definingourarray.

That defines the first array.

Your code to add to the array would overwrite the original array.

Zak
20-12-2011, 04:52 PM
O ok thank you :)

Want to hide these adverts? Register an account for free!