therealwesfoster Posted May 17, 2008 Share Posted May 17, 2008 You know how you can do this to add another key to the end of an array? <?php $ar = array(); $ar[] = "First entry"; $ar[] = "Second entry"; ?> Well, how would you do it for an array like below: <?php $ar = array("First" => "1st", "Second" => "2nd"); $ar[] = "Third" => "3rd"; ?> I know the way I did it was completely wrong, but hopefully you get what I'm trying to do. I would look through google for the answer, but I don't know what you would call that. In-line array pushing? lol thanks Link to comment https://forums.phpfreaks.com/topic/105999-solved-simple-array-question/ Share on other sites More sharing options...
DarkWater Posted May 17, 2008 Share Posted May 17, 2008 $arr['Third'] = "3rd"; >_> Link to comment https://forums.phpfreaks.com/topic/105999-solved-simple-array-question/#findComment-543245 Share on other sites More sharing options...
therealwesfoster Posted May 17, 2008 Author Share Posted May 17, 2008 Thanks for the reply and the weird face. I tried that though, and it doesn't work. What I did: <?php $template['assign']["PIC0"] = $val; $template['assign'] = array( "VAL2" => $val2, ); ?> I think the second declaration is overwriting the first. So how would I re-write the second array push? Wes Link to comment https://forums.phpfreaks.com/topic/105999-solved-simple-array-question/#findComment-543247 Share on other sites More sharing options...
DarkWater Posted May 17, 2008 Share Posted May 17, 2008 $template['assign']["PIC0"] = $val; $template ['assign']['VAL2'] = $val2; Uhh, yeah. Link to comment https://forums.phpfreaks.com/topic/105999-solved-simple-array-question/#findComment-543248 Share on other sites More sharing options...
therealwesfoster Posted May 17, 2008 Author Share Posted May 17, 2008 Enough with the sarcasm kid. I don't want to go through and have: $template['assign']['VAL3'] = $val3; $template['assign']['VAL4'] = $val4; $template['assign']['VAL5'] = $val5; etc. etc. etc. So how would I re-write the: <?php $template['assign'] = array( "VAL2" => $val2, "VAL3" => $val3 ); ?> etc. In order to push them Link to comment https://forums.phpfreaks.com/topic/105999-solved-simple-array-question/#findComment-543249 Share on other sites More sharing options...
MasterACE14 Posted May 17, 2008 Share Posted May 17, 2008 I don't really understand what you mean? you can always use a 2 dimensional or 3 dimensional array ACE Link to comment https://forums.phpfreaks.com/topic/105999-solved-simple-array-question/#findComment-543251 Share on other sites More sharing options...
947740 Posted May 17, 2008 Share Posted May 17, 2008 I think none of us understand what he means. Link to comment https://forums.phpfreaks.com/topic/105999-solved-simple-array-question/#findComment-543252 Share on other sites More sharing options...
therealwesfoster Posted May 17, 2008 Author Share Posted May 17, 2008 How can I use something like array_push() for associative arrays? And be able to push multiple associative arrays at once (in the same function) Link to comment https://forums.phpfreaks.com/topic/105999-solved-simple-array-question/#findComment-543253 Share on other sites More sharing options...
DarkWater Posted May 17, 2008 Share Posted May 17, 2008 Sarcasm? Nah. And appreciate it when people help you. Got it, kid? $toadd = array("VAL2" => $val2, "U900" => $val3); $arr = array("First" => "1st"); foreach ($toadd as $k=>$v) { $arr[$k] = $v; } Link to comment https://forums.phpfreaks.com/topic/105999-solved-simple-array-question/#findComment-543255 Share on other sites More sharing options...
DarkWater Posted May 17, 2008 Share Posted May 17, 2008 function array_push_assoc(&$arr) { if (function_num_args() > 1) { $arrays = func_get_args(); foreach ($arrays as $k=>$v) { if ($k==0){continue;} foreach ($v as $key=>$val) { $arr[$key] = $val; } } } } Gotta love knowing how to make variable parameter lists. And if you're a jerk again, or you're so arrogant as to call me a kid, expect no help. Link to comment https://forums.phpfreaks.com/topic/105999-solved-simple-array-question/#findComment-543261 Share on other sites More sharing options...
therealwesfoster Posted May 17, 2008 Author Share Posted May 17, 2008 function array_push_assoc(&$arr) { if (function_num_args() > 1) { $arrays = func_get_args(); foreach ($arrays as $k=>$v) { if ($k==0){continue;} foreach ($v as $key=>$val) { $arr[$key] = $val; } } } } Gotta love knowing how to make variable parameter lists. And if you're a jerk again, or you're so arrogant as to call me a kid, expect no help. That'll work man. Much appreciated. And as for the comment, you'll learn in life that some things just aren't worth dwelling on. Thanks again Link to comment https://forums.phpfreaks.com/topic/105999-solved-simple-array-question/#findComment-543266 Share on other sites More sharing options...
DarkWater Posted May 17, 2008 Share Posted May 17, 2008 Glad I could help. Link to comment https://forums.phpfreaks.com/topic/105999-solved-simple-array-question/#findComment-543268 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.