PHP Arrays Tutorial and PHP Array Examples

An array in PHP is actually an ordered map. A map is a type that maps values to keys. This type is optimized in several ways, so you can use it as a real array, or a list (vector), hashtable (which is an implementation of a map), dictionary, collection, stack, queue and probably more. Because you can have another PHP array as a value, you can also quite easily simulate trees.

PHP Array Syntax: Create an Array
language-construct array() is used to create an array in PHP. See example below

array( [key =>] value
  , …
  )
key: key may be an integer or string
value: A value can be of any PHP type

Examples
$arr = array(“foo” => “bar”, 12 => true);
echo $arr["foo"]; this will print bar
echo $arr[12]; this will print 1

if you provide the brackets with no key specified, then the maximum of the existing integer indices +1 is taken as key. see below

$arr = array(5 => 1, 12 => 2); This will create an array with 2 elements
$arr[] = 56;     new key will be maximum key + 1 i.e $arr[13] = 56
$arr["x"] = 42;  This adds a new element to the array with key “x”

array(5 => 43, 32, 56, “b” => 12); This array is the same as following.
array(5 => 43, 6 => 32, 7 => 56, “b” => 12);

Handling arrays from html form inputs to php scripts
Following example will show we can use an array from html form inputs.

HTML form with array
<input type=”checkbox” name=”selected_ids[]” value=”1″>
<input type=”checkbox” name=”selected_ids[]” value=”2″>
<input type=”checkbox” name=”selected_ids[]” value=”3″>
<input type=”checkbox” name=”selected_ids[]” value=”11″>
<input type=”checkbox” name=”selected_ids[]” value=”12″>
<input type=”checkbox” name=”selected_ids[]” value=”13″>

When we submit above form, it will generate $_POST['selected_ids'][] array to the form handling php script. This array holds all selected checkbox values from above html form. foreach() construct can be used to extract values from the array. Following code example will show how we can extract those values from the returning array.

foreach ($_POST['selected_ids'] as $key => $value) {
    echo “Key: $key; Value: $value<br>”;
}
for example if 1,2 and 12 is selected from the above html form then above code will print
Key: 0 Value: 1
Key: 1 Value: 2
Key: 2 Value: 12

About these ads

33 Responses to PHP Arrays Tutorial and PHP Array Examples

  1. someguy says:

    Just wanted to say thanks for this tutorial. Been beating my head against the wall with arrays. Have read a number of other tutorials. I don’t know what it is about yours but after reading it I was able to accomplish what I needed. Thanks!

  2. Mohammad Arif Hossen says:

    Thanks

  3. oni-kun says:

    Thanks alot, I was wondering about setting arrays with form data.. This was better than other tutorials.

  4. kriszelle says:

    great!!!!

  5. jobvacancies says:

    thank you for the information,
    This was helped,
    spirit! ! ! ! !

  6. Thiagarajan says:

    I am trying to pass an array to a checkbox post variable. While retriving the content of the array I am getting warning invalid argument. The code is below. could any one help??

    selectproducts();
    //$sc->test();

    print “”;

    foreach($t as $v)
    {
    for($i=0; $i< count($v); $i++)
    {
    echo $v[$i];
    }
    print"

    “;
    }
    print”

    “;

    }

    ?>
    getstring();

    ?>

    product = trim($tempproduct);
    $this->qty = trim($tempqty);

    echo $this->product;
    echo $this->qty;
    }

    public function getstring()
    {

    foreach($this->product as $v)
    {
    print $v;
    }

    }

    ?>

  7. aaaaaaa says:

    tryrtyrtb dfgd dfgdfg dg

  8. PtF says:

    Thx for the tutorial. I’m making my own website and I have a doubt how can I add this search stuff that appears here in the top right corner?

  9. tania says:

    This is a nice article..
    Its very easy to understand ..
    And this article is using to learn something about it..

    c#, dot.net, php tutorial, Ms sql server

    Thanks a lot..!

  10. If you are looking in return a personality to secure spondulix at teleflora, coupon codes are the character to go. The are affirmed prohibited to alleviate sponsor products that companies are trying to promote. For a coterie like teleflora that sells flowers, the coupon jus civile ‘civil law’ functions to remedy customers shield money on flowers and gifts that they be afflicted with on loved ones.

    Teleflora Coupons

  11. Praveen says:

    nothing good

  12. mafri says:

    mafri…

    [...]PHP Arrays Tutorial and PHP Array Examples « PHP Programming[...]…

  13. geekeries says:

    geekeries…

    [...]PHP Arrays Tutorial and PHP Array Examples « PHP Programming[...]…

  14. [admission requirements|associate degree nursing program|bridge program|content bridge|educational mobility|nurse program|physical therapist assistant|practical nurse|registered nurse]…

    [...]PHP Arrays Tutorial and PHP Array Examples « PHP Programming[...]…

  15. Wittaya says:

    Thank you, very much.

  16. Webdesignérska firma zaoberajúca sa tvorbou www stránok a grafiky…

    [...]PHP Arrays Tutorial and PHP Array Examples « PHP Programming[...]…

  17. scripts, themes…

    [...]PHP Arrays Tutorial and PHP Array Examples « PHP Programming[...]…

  18. tushar mondal says:

    two associative array merge if key are same then value are add and print the array. how is possible?

  19. mikan says:

    guys, I have this code.
    for($x=0;$x<$num;$x++){
    if(is_numeric ($string[$x])){

    what is the use of ($string[$x])?? Thanks a lot.

  20. anupal says:

    am getting error
    Notice: Undefined index: name
    name is the field name as name[]
    i want to fetch array form data but getting this error
    please help

  21. Mathias says:

    vielen dank aus germany

  22. http://Www.injurysolicitors.com/caraccidentclaims.html You’r text helped me a lot, thank you for that, gonna save it, bookmarking that page at the moment!

  23. ranacseruet says:

    Great basics covering for php arrays. For the functinal references. I was wondering if you could add some details on multi-dimensional array as well. That should make this basics tutorial more rich. Besides, I will like to add some most frequently used php array functions reference which will be helpful to readers as next refernece, I guess. Thanks.

  24. I was wondering if you ever thought of changing the layout
    of your website? Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could
    connect with it better. Youve got an awful lot of text for only having one
    or two pictures. Maybe you could space it out better?

  25. Hi, There’s no doubt that your site could possibly be having web browser compatibility problems. Whenever I take a look at your web site in Safari, it looks fine however, if opening in Internet Explorer, it’s got some
    overlapping issues. I merely wanted to give you a
    quick heads up! Other than that, wonderful blog!

  26. Woah! I’m really digging the template/theme of this site.

    It’s simple, yet effective. A lot of times it’s tough to get that “perfect balance” between usability and appearance.

    I must say you have done a awesome job with
    this. Also, the blog loads super fast for me on Internet
    explorer. Excellent Blog!

  27. Excellent information.Hope to get more in future.

  28. What’s up Dear, are you really visiting this website on a regular basis, if so afterward you will definitely get
    nice know-how.

  29. Frankie says:

    I was more than happy to find this page. I wanted to thank you for your time due
    to this wonderful read!! I definitely appreciated every bit of it and
    i also have you saved to fav to check out new
    stuff in your website.

  30. Judi says:

    Hey, I think your site might be having browser compatibility issues.

    When I look at your website in Firefox, it looks fine but when opening
    in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up!
    Other then that, fantastic blog!

  31. Drusilla says:

    Nice replies in return of this issue with genuine
    arguments and explaining everything about that.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: