Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add to an array's bottom 1

Status
Not open for further replies.

timgerr

IS-IT--Management
Jan 22, 2004
364
US
I was wondering if there was a way to add to the bottom of an array?
Code:
$a = array();
$a[] = 'timmm';
$a[] = 'Tim';
$a[] = 'tom';
$a[] = 'Tom';	
print_r($a);

Array ( [0] => timmm [1] => Tim [2] => tom [3] => Tom )
I would like to change the order with out using array_reverse. I want all new elements to be $a[0]. Can this be done?

Thanks,
timgerr

-How important does a person have to be before they are considered assassinated instead of just murdered?
So there you go! You're the retarded offspring of five monkeys having butt sex with a fish-squirrel! Congratulations!


 
While I don't think there's a built in function for it. Something like this may be what you are looking for.



Code:
function array_insert($arr,$newvalue){
$newarr=array(); [green]//Create new array[/green]
$newarr[0]=$newvalue; [green]Insert new item into first key of array.[/green]
foreach($arr as $entry){
array_push($newarr,$entry);
}
[green]insert contents of old array into new array[/green]
return $newarr; [green]return new array.[/green]

}

Obviously this requires some error checking. Verify the variable passed is really an array, and stuff like that, but Its the basic concept.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
The functions array_pad or array_push will probably help you.
array_unshift will add an element to beginning of an array.
 
Thanks, array_unshift did the trick.

Timgerr

-How important does a person have to be before they are considered assassinated instead of just murdered?
So there you go! You're the retarded offspring of five monkeys having butt sex with a fish-squirrel! Congratulations!


 
I want all new elements to be $a[0]

given that this is contrary to normal coding practice in most (all?) languages that support arrays, i'd be very interested to know why you want your code to behave like this. can you elucidate?
 
I am using a stack that I need to have the newest addition first.


I guess I am new, why is "this", "contrary to normal coding practice"

Thanks,
timgerr

-How important does a person have to be before they are considered assassinated instead of just murdered?
So there you go! You're the retarded offspring of five monkeys having butt sex with a fish-squirrel! Congratulations!


 
What was probably meant is that the traditional way of doing things would be to treat the array as a stack, whereby you constantly add things to the "top" or end of the array. Because of how arrays are normally stored in terms of linking from one element to another, it often makes more sense to add to the back end and not the front. If the location of the array remains constant and you really are adding to the front or 0th element, you may be making the computer do a number of unnecessary shifts (slowing your code down).

Hope that helps.

- Flub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top