Welcome to Geeklog, Anonymous Wednesday, April 24 2024 @ 10:09 pm EDT

Geeklog Forums

Insert an array in PHP not at end/start


Status: offline

LWC

Forum User
Full Member
Registered: 02/19/04
Posts: 818
I know it's more PHP related that Geeklog related, but can you please tell me if I can insert one array inside another, but not at the other's end/start and without knowing the numeric position of where I want to insert it?

It's based on machinari's tip to use an array for $message for an endless possibilities' contact form. For some long reason, I need a certain spelling for my array keys in the contact form and another in my e-mail message...

For example,
print_r($array1);
Array
(
["test_one"] => whatever
["test_tow"] => whatever
["test_three"] => whatever
)

Oops (again, not really), I wrote "tow"...now I want to rename it right, but, as far as I can tell, PHP won't let rename array keys.

So I just want to insert "test_two" before/after the "test_tow", only it's dynamic (because I have many of these fields and I have an automatic loop that goes through the array).
Anyway, if I manage to do that, then I'd use:

unset($array1["test_tow"];

and therefore "test_two" would lie safely in the position which once belonged to "test_tow" (position 1 - but only in this specific example).
In other words, I'd end up with:
print_r($array1);
Array
(
["test_one"] => whatever
["test_two"] => whatever
["test_three"] => whatever
)

See how not only "test_tow" turned into "test_two", but also took its position?

Can I do that (or better yet, simply rename array keys)?

Thanks!
 Quote

Status: offline

vadertech

Forum User
Full Member
Registered: 05/26/03
Posts: 329
If you dont get an answer here, you should try here.
GeekLog Hosting, Installations and Upgrades - WWW.AWEHOST.COM - Hosting starts @ only $4.95/mo.
 Quote

Status: offline

LWC

Forum User
Full Member
Registered: 02/19/04
Posts: 818
Ok, Here it is there.

Meanwhile, I've come up with a solution - using array_flip - that way I can get the old key's offset and, once I have the offset, use some array functions to split and merge at the correct spot.
To those who need them, find more details at the linked post.
 Quote

All times are EDT. The time is now 10:09 pm.

  • Normal Topic
  • Sticky Topic
  • Locked Topic
  • New Post
  • Sticky Topic W/ New Post
  • Locked Topic W/ New Post
  •  View Anonymous Posts
  •  Able to post
  •  Filtered HTML Allowed
  •  Censored Content