Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-20-2012, 11:09 PM   PM User | #1
comport9
New Coder

 
Join Date: Oct 2012
Posts: 25
Thanks: 2
Thanked 0 Times in 0 Posts
comport9 is an unknown quantity at this point
Adding elements to a list within an array

I'm somewhat familiar with python, and so am still getting by barrings with PHP.

I have an array with named keys. Each key's value is a list of items. How do I add items to the value?

Array
"Cars" => "1, 12, 22";
"Trucks" => "3, 5, 18, 9";
"Vans" => "5";

Say I want to add "17" to "Cars", how would I do that?

Thanks!
comport9 is offline   Reply With Quote
Old 11-20-2012, 11:13 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
PHP Code:
$a = array(
    
'Cars' => array(11222),
    
'Trucks' => array(35189),
    
'Vans' => array(5)
);

// Add 17 to cars:
$a['Cars'][] = 17// treats it similar to push.
array_unshift($a['Cars'], 17);
array_push($a['Cars'], 17); 
Simple as that.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
comport9 (11-20-2012)
Old 11-20-2012, 11:27 PM   PM User | #3
comport9
New Coder

 
Join Date: Oct 2012
Posts: 25
Thanks: 2
Thanked 0 Times in 0 Posts
comport9 is an unknown quantity at this point
Thanks huge man!
comport9 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:58 AM.


Advertisement
Log in to turn off these ads.