PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 15th, 2012, 02:19 PM
pgrinPDT's Avatar
pgrinPDT pgrinPDT is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2008
Posts: 576 pgrinPDT User rank is First Lieutenant (10000 - 20000 Reputation Level)pgrinPDT User rank is First Lieutenant (10000 - 20000 Reputation Level)pgrinPDT User rank is First Lieutenant (10000 - 20000 Reputation Level)pgrinPDT User rank is First Lieutenant (10000 - 20000 Reputation Level)pgrinPDT User rank is First Lieutenant (10000 - 20000 Reputation Level)pgrinPDT User rank is First Lieutenant (10000 - 20000 Reputation Level)pgrinPDT User rank is First Lieutenant (10000 - 20000 Reputation Level)pgrinPDT User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 6 h 23 m 43 sec
Reputation Power: 180
Merging Associative Arrays with Overwrite/Append

Alright, I've been around long enough where I should know how to do this by now. I have worked around this in the past by looping through both arrays and coming up with a third array that contains the appropriate values. So here is my test data...

PHP Code:
 $defaults = Array(
   
'attr'=>Array(
      
'class'=>Array('beta')
      ,
'id'=>'beta'
   
)
);

$newArray = Array(
  ,
'attr'=> Array(
        
'id'=>'test'
  
),'style' => Array(
      
'text-align'=>'left'
  
)
);

//I want the defaults array to change beta to test and append
//the style on it... so the defaults array ends up looking like

//Array(id=>test, attr=>Array(class=>Array(beta),id=>test),
//style=>Array(text-align=>left) 


I have tried array_merge and adding the arrays together. Adding the arrays appends the style which works but I can't figure out how to overwrite the 'id' value without looping through the array.

Is there any tips on how to accomplish this more efficiently?

Last edited by pgrinPDT : October 15th, 2012 at 02:22 PM.

Reply With Quote
  #2  
Old October 15th, 2012, 02:48 PM
Jacques3 Jacques3 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 57 Jacques3 User rank is Sergeant (500 - 2000 Reputation Level)Jacques3 User rank is Sergeant (500 - 2000 Reputation Level)Jacques3 User rank is Sergeant (500 - 2000 Reputation Level)Jacques3 User rank is Sergeant (500 - 2000 Reputation Level)Jacques3 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 10 h 6 m 14 sec
Reputation Power: 11
Hi,

I fear there is no built-in function for this. I thought of array_merge_recursive(), but this works very different (scalar values are joined in an array instead of overwritten).

So you'll either have to use a loop or recursion.

Reply With Quote
  #3  
Old October 15th, 2012, 05:47 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,947 E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 10 h 37 m 24 sec
Reputation Power: 7053
I agree with Jacques, I don't believe there is any built in function with that exact logic. You could file it as a feature request against array_merge_recursive (as an optional parameter), it sounds like something that has a decent chance of getting added eventually.
__________________
PHP FAQ
How to program a basic, secure login system using PHP
Connect with me on LinkedIn


Quote:
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

Reply With Quote
  #4  
Old October 16th, 2012, 10:54 AM
pgrinPDT's Avatar
pgrinPDT pgrinPDT is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2008
Posts: 576 pgrinPDT User rank is First Lieutenant (10000 - 20000 Reputation Level)pgrinPDT User rank is First Lieutenant (10000 - 20000 Reputation Level)pgrinPDT User rank is First Lieutenant (10000 - 20000 Reputation Level)pgrinPDT User rank is First Lieutenant (10000 - 20000 Reputation Level)pgrinPDT User rank is First Lieutenant (10000 - 20000 Reputation Level)pgrinPDT User rank is First Lieutenant (10000 - 20000 Reputation Level)pgrinPDT User rank is First Lieutenant (10000 - 20000 Reputation Level)pgrinPDT User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 6 h 23 m 43 sec
Reputation Power: 180
Thanks E-Oreo, Jacques.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Merging Associative Arrays with Overwrite/Append

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap