...

View Full Version : Adding elements to a list within an array



comport9
11-20-2012, 11:09 PM
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!

Fou-Lu
11-20-2012, 11:13 PM
$a = array(
'Cars' => array(1, 12, 22),
'Trucks' => array(3, 5, 18, 9),
'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.

comport9
11-20-2012, 11:27 PM
Thanks huge man!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum