rvinikof Posted July 6, 2007 Share Posted July 6, 2007 how do i assign multiple values to multiple elements? ex. $price = array("item" => 10, 15, 16, => "item2" => 13, 14, 15); i thought this would work, but when i try to show something like: item costs 10, 15, 16. item2 costs 13, 14, 15. instead i get: item costs 10. 0 costs 15. 1 costs 16. item2 costs 13. 2 costs 14. 3 costs 15. Link to comment https://forums.phpfreaks.com/topic/58778-associate-arrays/ Share on other sites More sharing options...
Barand Posted July 6, 2007 Share Posted July 6, 2007 <?php $price = array( "item" => array(10, 15, 16), "item2" => array(13, 14, 15) ); ?> Link to comment https://forums.phpfreaks.com/topic/58778-associate-arrays/#findComment-291601 Share on other sites More sharing options...
per1os Posted July 6, 2007 Share Posted July 6, 2007 $price = array("item" => array(10, 15, 16), => "item2" => array(13, 14, 15)); Link to comment https://forums.phpfreaks.com/topic/58778-associate-arrays/#findComment-291603 Share on other sites More sharing options...
rvinikof Posted July 6, 2007 Author Share Posted July 6, 2007 but then how do i print out the three numbers for each item? when i use the foreach() i get item costs Array. item2 costs Array. Link to comment https://forums.phpfreaks.com/topic/58778-associate-arrays/#findComment-291604 Share on other sites More sharing options...
Barand Posted July 6, 2007 Share Posted July 6, 2007 <?php $price = array( "item" => array(10, 15, 16), "item2" => array(13, 14, 15) ); foreach ($price as $item => $prices) { echo $item . ' costs: '; foreach ($prices as $p) { echo " $p"; } echo '<br>'; } ?> Link to comment https://forums.phpfreaks.com/topic/58778-associate-arrays/#findComment-291605 Share on other sites More sharing options...
teng84 Posted July 6, 2007 Share Posted July 6, 2007 $price = array( "item" => array(10, 15, 16), "item2" => array(13, 14, 15)); foreach($price as $key=>$value) { echo $key; foreach($value as $astig) { echo $astig; } } ops same but ive already made it so i posted it then Link to comment https://forums.phpfreaks.com/topic/58778-associate-arrays/#findComment-291607 Share on other sites More sharing options...
Barand Posted July 6, 2007 Share Posted July 6, 2007 Alternatively <?php $price = array( "item" => array(10, 15, 16), "item2" => array(13, 14, 15) ); foreach ($price as $item => $prices) { echo $item . ' costs: ' . join(', ', $prices) . '<br>'; } ?> Link to comment https://forums.phpfreaks.com/topic/58778-associate-arrays/#findComment-291611 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.