echo and add array keys

109 Views Asked by At

My Array:

<?php
    $fruitcolors = array('apple'=>'red', 'lemon'=>'yellow');
?>

I need to know how to echo the value of apple, so it echos "red".

Also I need to know how to add a new value with php code:

$fruitcolors[] = "Pear" => "Green"; etc.

3

There are 3 best solutions below

0
On
<?php
    // echo 'red'
    echo $fruitcolors['apple'];

    // assign key: 'Pear', value: 'Green'
    $fruitcolors['Pear'] = 'Green';
?>    

See http://php.net/array for more info.

0
On

Basic feature of arrays are clearly explained in the manual:

echo $fruitcolors['apple'];
$fruitcolors['pear'] = 'green';
0
On

If you google "php arrays" this is likely among the results:

http://php.net/manual/en/language.types.array.php