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 Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 17, 2008 Share Posted May 17, 2008 $arr['Third'] = "3rd"; >_> Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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) Quote Link to comment 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; } Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 17, 2008 Share Posted May 17, 2008 Glad I could help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.