Forum Moderators: coopster

Message Too Old, No Replies

Add element onto array

         

bobnew32

10:51 pm on Dec 1, 2004 (gmt 0)

10+ Year Member



I have an array that has been already created from a script. Here is the code for that array:

$returnArray[]= array("id" => $smilieRow['id'],
"url" => $smilie->url,
"bbcode" => $smilie->bbcode);

I have another array too that is created pretty similarly to that. What I want to do is add another element to the array called "$num". Now I would just loop through it again with a foreach, but is there an easier way?

dreamcatcher

1:09 am on Dec 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can you not just do something like:

$returnArray[] = "$num";

or whatever your other array is called. If you want to add a new element to the end of the array, use array_push. If you want to add the element to the beginning, use array_unshift.

dc

coopster

1:27 am on Dec 2, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Also keep in mind that arrays are made up of key=value pairs. The array you show here is what is called a multidimensional array -- one of the elements of the array is actually another array. print_r() [php.net] comes in very handy when troubleshooting arrays.