Download Horizon :: Staff Members :: TheTechGame.com :: XboxMB YouTube


Old 08-21-2010   #1 (permalink)
Banned
 
Join Date: Jul 2010
Location: Cambridge, MA
Posts: 3,138
Thanks: 4,805
Lightbulb Arrays

Arrays
An array is a fixed list of values which can be accessed with their corresponding key. By default the keys are assigned an integer, starting from 0 and going up in 1's, but, you can give them their own unique name which we will get into later. Each value in the list is seperated by a comma, the values can be any kind of data - strings, integers and so on. You can even have arrays inside of arrays which, again, we will get into later.

Below we are simply creating an array and naming it $myArray. This array will have 11 values assigned in it. In each we will have an integer or a string.

PHP Code:
$myArray = array(4145635"dean"1917"dog"213513); 

Now, to retrieve a value we specify the name we gave to the array and the key assigned to the value:

PHP Code:
echo $myArray[2]; 

We named our array $myArray, so, to retrieve data from it we give the name we gave to the array and the specific key in which has been assigned to it.

The above example will echo out the data in key name 2 which is 56.
Why 56? Array keys start from 0 not from 1 so if we wanted to get the number 4, which has the key 0 since it is the first value we identified in the array we would do the following:

PHP Code:
echo $myArray[0]; 



Arrays inside of Arrays
Again let's firstly create our array except this time we're going to have an array inside of it.

PHP Code:
$myOtherArray = array(1234, array("a""b""c")); 

Retrieving values from an array inside of an array is just as simple as well. We named this array $myOtherArray, so, if we want to get values from the array inside of it we do this:

PHP Code:
echo $myOtherArray[4][1]; 

The key named 4 contains our array so we specify that first then we specify the key in which the value is we want to obtain, which is 1. This will echo 'b' back to us.



Giving Keys Names
Giving keys their own unique names.

In this case, 'my_name' will contain the string "Dean" and 'number' will contain the integer 1.

PHP Code:
$namedArray = array("my_name" => "Dean""number" => 1); 

To retrieve the data from a named array we do the following:

PHP Code:
echo $namedArray["my_name"]; 

This will echo back to us what's in key named 'my_name', in this case, Dean. We can do the exact same for retrieving the data in any other named key:

PHP Code:
echo $namedArray["number"]; 


Adding to arrays
Like everything before, this is just as simple. Here's how it's done...

Let's define our array first.
PHP Code:
$addToArray = (1"dog"34"dean"); 
Now to add data to an array, we do this.
PHP Code:
//$addToArray will be the array name
$addToArray[] = 11;
$addToArray[] = "hey"
Above, we added 2 pieces of data to the array 11 and hey.
The array will now look like this...
PHP Code:
$addToArray[0]; //Outputs 1
$addToArray[1]; //Outputs dog
$addToArray[2]; //Outputs 3
$addToArray[3]; //Outputs 4
$addToArray[4]; //Outputs dean
//Now our new pieces of data, it incremements by 1 each time, so, 5 will output 11 and 6 will output hey
$addToArray[5]; //Outputs 11
$addToArray[6]; //Outputs hey 


PHP 5.4 Arrays
As of PHP 5.4, you can define arrays using a shorter syntax.

PHP Code:
$xboxMb = ['XboxMB''is''epic'];
 
$userGroups = [
    
=> 'Guest',
    
=> 'Diamond',
    
=> 'Developer'
];

$students = [
    
648 => [
        
'name' => 'John Smith',
        
'average' => 87.5
    
],
    
998 => [
        
'name' => 'Derp Derp',
        
'average' => 31.0
    
]
]; 
Dean is offline Send a message via AIM to Dean Send a message via Yahoo to Dean Send a message via Skype™ to Dean
Reply With Quote


Old 08-21-2010   #2 (permalink)
Cheater912's Avatar
:D
Join Date: Jul 2010
Location: New York
Posts: 5,683
Thanks: 12,223
Default Re: Arrays

Awesome post! This will help a lot of newbz.
__________________
Cheater912 is offline Send a message via Skype™ to Cheater912
Reply With Quote


Old 09-25-2010   #3 (permalink)
Regular Member
 
Join Date: Sep 2010
Location: Vermont
Posts: 34
Thanks: 2
Default Re: Arrays

Quote:
Originally Posted by Cheater912 View Post
Awesome post! This will help a lot of newbz.
And/or someone who likes to steal codes and give no credit...either way lol.
Eminem07481 is offline
Reply With Quote


Old 09-26-2010   #4 (permalink)
Banned
 
Join Date: Jul 2010
Location: Cambridge, MA
Posts: 3,138
Thanks: 4,805
Default Re: Arrays

Quote:
Originally Posted by Eminem07481 View Post
And/or someone who likes to steal codes and give no credit...either way lol.
Why would I need credit? This is just a post showing how to use arrays lol. I didn't create them.
Dean is offline Send a message via AIM to Dean Send a message via Yahoo to Dean Send a message via Skype™ to Dean
Reply With Quote


Old 09-26-2010   #5 (permalink)

paintlax21's Avatar
Join Date: Sep 2010
Location: New York
Posts: 1,346
Thanks: 366
Default Re: Arrays

I love arrays
paintlax21 is offline Send a message via AIM to paintlax21 Send a message via MSN to paintlax21
Reply With Quote
The following user thanked this post: Dean




Old 09-26-2010   #6 (permalink)
Regular Member
 
Join Date: Sep 2010
Posts: 226
Thanks: 49
Default Re: Arrays

Quote:
Originally Posted by Eminem07481 View Post
And/or someone who likes to steal codes and give no credit...either way lol.
wtfamireading.jpg

OH GOD YOU USED AN ARRAY AND DIDN'T CREDIT ME WTF

lol

Nice job, Dean.
Michael Allison is offline
Reply With Quote
The following user thanked this post: Dean


Old 10-12-2010   #7 (permalink)
Banned
 
Join Date: Oct 2010
Location: Pennsylvania
Posts: 355
Thanks: 192
Default Re: Arrays

Good post.
Mocha is offline Send a message via AIM to Mocha
Reply With Quote


Old 10-12-2010   #8 (permalink)
Regular Member
Avery's Avatar
Join Date: Sep 2010
Location: This Forum
Posts: 148
Thanks: 20
Default Re: Arrays

Great post man (as always)
__________________
If someone helps click the "Thanks" button!

Have a question about the site? Feel free to PM me!
Avery is offline Send a message via AIM to Avery
Reply With Quote


Old 10-18-2010   #9 (permalink)
Regular Member
TTG Prodigy's Avatar
Join Date: Oct 2010
Location: At nichole's house
Posts: 295
Thanks: 76
Default Re: Arrays

Good job
__________________

TTG Prodigy and Nookie and Dean are modders for life XeX >_< O_o
TTG Prodigy is offline Send a message via AIM to TTG Prodigy
Reply With Quote
The following user thanked this post: TTG NiNjA


Old 10-31-2010   #10 (permalink)

debug's Avatar
Join Date: Oct 2010
Posts: 212
Thanks: 50
Default Re: Arrays

What about adding to an array, like doing

$myArray = array(1=>'value 1',2=>'value 2');
$myArray[] = 'value 3';

$myArray will now be:
$myArray = array(1=>'value 1',2=>'value 2',3=>'value 3');

and nice tutorial
debug is offline
Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 09:17 AM.


 

Powered by vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
COPYRIGHT (c) 2010 - 2012 - XboxMB - DESIGN BY:
EDENWEBS.COM