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. Quote Link to comment 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) ); ?> Quote Link to comment 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)); Quote Link to comment 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. Quote Link to comment 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>'; } ?> Quote Link to comment 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 Quote Link to comment 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>'; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.