Gray Hat World - Internet Marketing Forum
    home search help facebook twitter

Gray Hat World - Internet Marketing Forum » General Computer Discussion » Graphics And Design » Website Construction » PHP/HTML/CSS » PHP Working with arrays
Current time: 05-18-2013, 06:01 PM Hello There, Guest! (Login — Register)

Register for full access and no ads



Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Threaded Mode | Linear Mode
PHP Working with arrays
12-22-2009, 11:01 AM
Post: #1
duronrulez Offline
Litte Baby
Posts: 5
Joined: Dec 2009
Reputation: 2
Experience: 11
PHP Working with arrays
Hello!
Its me again!
With yet another useful tutorial for php. This time we will take a little look at the arrays in php. How to use, them, and not get scared!

Lets start with the simplest example:
Code:
<?php
$arr = array("myarraykey" => "myarrayvalue", 2 => true);

echo $arr["myarraykey"]; // myarrayvalue
echo $arr[2];    // 1
?>
What does the code above do?
Well its pretty simple. First we create a new array with 2 values. Then we display the values in the browser.

Each value in array has a key, array keys are optional, if not declared they are automatically incremental, starting from 0, meaning that if we do only:
Code:
$arr = array(1,2);
Our array will contain the values of 1 and 2, in the respective keys 0 and 1.
So if we follow that logic if we do:
Code:
$arr = array(1,2);
echo $arr[0]; // 1
echo $arr[1]; // 2
The code above will display to the browser the 2 values of the array. We get the values by the keys associated with them. In this case the auto generated incremental keys Smile In the first example we created a so called associative array. Meaning that we put a "known" name to each key, for the respective value.
This way is preferred for us if we describe stuff, like objects, items, etc.
And the auto incremental keys are preferred when we have multiple arrays that we like to be in a array Smile (i know it sounds complicated but bare with me).
So an example is needed to clarify things Smile
Code:
$arr = array(array('name' => 'john', 'age' => 20), array('name' => 'bruce', 'age' => '15'));
echo $arr[0]['name']; // john
echo $arr[1]['name']; // bruce
echo $arr[0]['age']; // 20
Basically its like a list with candidates Smile Each item on the list has several properties, in our case the properties are name and age.

Again this is a very basic usage case. Arrays are very powerful, they can represent basically any set of items, objects, persons and so on. Mastering them is very important for knowing your way around in solving php problems Smile
Find all posts by this user
Quote this message in a reply
12-22-2009, 12:01 PM
Post: #2
Macdonjo Offline
Administrator
*******
Administrators
Posts: 1,321
Joined: Jul 2009
Reputation: 5
Experience: 12178
Diamond Member
RE: PHP Working with arrays
Good tutorial, +reped! Smile

Have a nice day.

Please check the Announcements and News section of the forum, there are great events and important news waiting for you.

Please register and check the site regularly for updates. Please bookmark us! Thank-you very much!
Find all posts by this user
Quote this message in a reply
01-09-2010, 03:11 AM
Post: #3
NzElite
Unregistered
 
RE: PHP Working with arrays
Nice Tut
Quote this message in a reply
04-12-2010, 04:45 AM
Post: #4
aaronaugusts Offline
Litte Baby
Posts: 5
Joined: Apr 2010
Reputation: 0
Experience: 30
RE: PHP Working with arrays
With PHP, we only have arrays. They are very simple. It is something like a list of items, where each entry has two properties: A key and a value. If you have the key, you can access a value directly - otherwise, you will have too look for it. PHP allows us unlimited dimensions of arrays, which means that we can have an array that has a range of arrays, which have a range of arrays, and so on.

Defeat is not bitter unless you swallow it.
Find all posts by this user
Quote this message in a reply
08-04-2011, 10:21 AM
Post: #5
Sarlotanas Offline
Litte Baby
Posts: 5
Joined: Aug 2011
Reputation: 0
Experience: 25
RE: PHP Working with arrays
Yeah Nice Tut Ill Try.
Find all posts by this user
Quote this message in a reply
08-15-2011, 03:03 PM
Post: #6
frostiee Offline
Litte Baby
Posts: 6
Joined: Aug 2011
Reputation: 0
Experience: 30
RE: PHP Working with arrays
Perfect TUTorial
Find all posts by this user
Quote this message in a reply
« Next Oldest | Next Newest »
Post Reply 


  • View a Printable Version
  • Send this Thread to a Friend
  • Subscribe to this thread
Forum Jump:


User(s) browsing this thread: 1 Guest(s)

Contact Us | Gray Hat World - Internet Marketing Forum | Return to Top | Return to Content | Lite (Archive) Mode | RSS Syndication

Powered By MyBB, © 2002-2013 MyBB Group.
Theme By GFX Palace.