Jump to content

associate arrays


rvinikof

Recommended Posts

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

<?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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.