AndrewDavidson.com.  We don't reinvent the wheel... we just make reinventing the wheel easier. Visit PDFCalendar.com for free customizable printable calendars.
Home
Resources
Software
Links
Articles
About
About Drew
Contact
Feedback
Subscribe to our Newsletter:
Printer-Friendly
© 1999-2014
Andrew Davidson.
All Rights Reserved.

Where is array_append in PHP?

Copyright © 2006 Andrew B. Davidson. All Rights Reserved.
Last Updated: December 2006

How can I append one array to another in PHP?

You may be wondering where the function array_append() is in PHP, or what would be the equivalent function to append one array to another.
Depending on what you want to do, you may want to use one of several functions in PHP. For appending one array to another, you should use array_merge
// create our arrays
$my_array1 = array ( 'a', 'b', 'c' );
$my_array2 = array ( 'd', 'e', 'f' );

// append one array to the other
$my_array = array_merge($my_array1, $my_array2);

// print the resulting array to see if it worked
print_r($my_array);

results in

Array 
( 
	[0] => a 
	[1] => b 
	[2] => c 
	[3] => d 
	[4] => e 
	[5] => f 
)
For creating a union of two arrays, you can use the + binary operator, or the += unary operator.
For more information, see this page of the PHP manual: PHP Array Operators.
The + and += array operators are available in all versions of PHP, so you should have no trouble using this in your PHP scripts.
I hope this information helps your PHP web development efforts!

How do I append an item to an array?

You may be wondering how to append an item to an array, not how to append one array to another. This can be done with the [] array syntax. For more information, see PHP Arrays.
// create an array
$my_array = array ( 'a', 'b', 'c' );

// now append a few items
$my_array[] = 'd';
$my_array[] = 'e';
$my_array[] = 'f';


// print the resulting array to see if it worked
print_r($my_array);

results in

Array 
( 
	[0] => a 
	[1] => b 
	[2] => c 
	[3] => d 
	[4] => e 
	[5] => f 
)

Comments?

If you have an comments or suggestions on this technique, you can email me at AndrewDavidson {at} AndrewDavidson {dot} com, or use my Feedback Form if you prefer.