Jump to content

[SOLVED] Simple array question


Recommended Posts

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

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

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

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.