Add values to arrays in PHP

Like in other programming and scripting languages, PHP arrays are used to store data used in your application or script. The most common case is using arrays as a kind of memory for values inside a script. You can add values to different types of arrays:

  • Simple array
    The most simple array type is the array with only numbered index keys. This array type can hold multiple values with index numbers assigned by the developer or by PHP. You can add the array values with or without a number or index.
  • Associative array
    This array type works almost the same as the simple array type, but in this case you have to add a string value as a key. Good examples of an associative array are the $_POST of $_GET vars created by a contact form.
  • Multidimensional array
    This array type is so complex as you need. You can assign multiple arrays as values and/or indexes. The most common example where you face a multidimensional array, is when you parse a XML structure.

PHP add array values

How to add values to an array with PHP?

You can add values to an array with PHP by passing single values or complete arrays. PHP has also multiple functions for array manipulations. But first I will show some examples about how I add array values with PHP code:

Add array values with keys to get more control

If you have a bigger array structure you need array keys if you would like to access single elements. Use numbers or strings as array keys.

Add complete arrays as values to an array in PHP

There are many cases were a simple array with values isn’t enough and you need to use multiple array structures.

Add array values from your MySQL database result

There are also situations were you like to use an array to store some values from a bigger database result set. For this example we add the same kind of data like in the previous example, but than dynamically.

Add array values with PHP functions

Sometimes it’s useful to use one the PHP functions whenever you need more control while adding array values. Here are some of them.

Using arrays in your PHP script is an essential way to solve “problems”. I’m using them very often because it’s easy and PHP can access them very fast. I hope my examples made it more clear how to add array values and how to use them in your script. Check also the array section from the PHP manual, the offer a lot of information and code examples.

If you like to check the examples in your PHP code editor, you download a .zip here.

4 thoughts on “Add values to arrays in PHP

  1. Thanks for the nice post. I think there is a typo in:
    $number = (range(0, 12);
    and it should be:
    $number = range(0, 12);

    • Vielen Dank Ralf :)

      • no problem :)

        There is another interesting effect which maybe is worth mentioning. When you run:

        http://pastebin.com/0kZG0S4N

        then the result is:

        10 – orange
        11 – cherry
        12 – lemon
        2 – apple

        One might expect that items with an integer index are inserted at the corresponding position, but this just does not happen in PHP. The item is always inserted at the end of the array.

      • Hi Ralf,

        you’re right, there is no auto-sort feature, you need to run a function like
        sort($fruit, SORT_NUMERIC) after new array elements are added (that might be a good thing, sometimes)

Comment Rules

Don’t post your code here, post your code block or snippet to pastebin and include the pastebin URL in your comment.

I delete all comments with non related links inside the comment text. Don't use keywords for the field of your real name (most people like to use your name for their answer). Keep your comment related to the topic, if your question is off-topic, please use the contact form instead.

Leave a Reply to Olaf Lederer Cancel reply

Your email address will not be published. Required fields are marked *

*
*
Website