Discover Habbo's history
Treat yourself with a Secret Santa gift.... of a random Wiki page for you to start exploring Habbo's history!
Happy holidays!
Celebrate with us at Habbox on the hotel, on our Forum and right here!
Join Habbox!
One of us! One of us! Click here to see the roles you could take as part of the Habbox community!


Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2010
    Location
    United Kingdom
    Posts
    846
    Tokens
    1,766
    Habbo
    triston220

    Latest Awards:

    Default [Tutorial] [PHP] Arrays

    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:

    PHP Code:
    $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:

    PHP Code:
    Echo $array[0]; 
    The above will output '1'. To the page.

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

    PHP Code:
    Echo $array[1]; 
    However, accessing a value in an array with a linked key is slightly different. We use the key's name:

    PHP Code:
    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.

    PHP Code:
    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.

    PHP Code:
    $array[] = "New Value"
    If you do want to link a key, these braces will need to be filled.

    PHP Code:
    $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:

    PHP Code:
    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.

    PHP Code:
    unset($array['My_Key']); 
    After unsetting an element from the array, always call the array_values() function.

    PHP Code:
    $array array_values($array); 
    This will reset the index of the array.

    I hope this tutorial is useful.

    moderator alert Thread moved by Jordan (Forum Super Moderator): From 'Designing & Development' to here as more suited
    Last edited by Jordan; 20-12-2011 at 04:16 PM.


    Quote Originally Posted by Jaaaack! View Post
    See, however much I want this, my girlfriend uses my PC too much, and I would be killed.





  2. #2
    Join Date
    Mar 2008
    Posts
    5,108
    Tokens
    3,780

    Latest Awards:

    Default

    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.

  3. #3
    Join Date
    Jan 2010
    Location
    United Kingdom
    Posts
    846
    Tokens
    1,766
    Habbo
    triston220

    Latest Awards:

    Default

    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.
    Last edited by triston220; 05-12-2011 at 03:11 PM.


    Quote Originally Posted by Jaaaack! View Post
    See, however much I want this, my girlfriend uses my PC too much, and I would be killed.





  4. #4
    Join Date
    Oct 2006
    Posts
    9,905
    Tokens
    26,858
    Habbo
    Zak

    Latest Awards:

    Default

    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!

  5. #5
    Join Date
    Jan 2010
    Location
    United Kingdom
    Posts
    846
    Tokens
    1,766
    Habbo
    triston220

    Latest Awards:

    Default

    Quote Originally Posted by Zak View Post
    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.


    Quote Originally Posted by Jaaaack! View Post
    See, however much I want this, my girlfriend uses my PC too much, and I would be killed.





  6. #6
    Join Date
    Oct 2006
    Posts
    9,905
    Tokens
    26,858
    Habbo
    Zak

    Latest Awards:

    Default

    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



  7. #7
    Join Date
    Jan 2010
    Location
    United Kingdom
    Posts
    846
    Tokens
    1,766
    Habbo
    triston220

    Latest Awards:

    Default

    Quote Originally Posted by Zak View Post
    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


    PHP Code:
    $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.


    Quote Originally Posted by Jaaaack! View Post
    See, however much I want this, my girlfriend uses my PC too much, and I would be killed.





  8. #8
    Join Date
    Oct 2006
    Posts
    9,905
    Tokens
    26,858
    Habbo
    Zak

    Latest Awards:

    Default

    O ok thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •