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
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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.