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

18 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?

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.