4
Contributors
9
Replies
44
Views
3 Years
Discussion Span
Last Post by filipgothic
1

Refer Click Here
for array sorting .
Use a for loop to iterate the array elements and create hexadecimal color value for display.

Edited by divyakrishnan

0

How can I change color for each word in array and display it?

<?php

$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
    echo "fruits[" . $key . "] = " . $val . "\n";
}

?>
2

you need to rewrite your array to something like below to associate every fruit to each color.

<?php

    echo '<b> Fruits </b> <br/>';

             $fruits = array('lemon'=>'#FFFF00','orange'=>'#FFA500', 'apple'=>'#FF0000');

    ksort($fruits);

    foreach($fruits as $k => $v){

        echo '<p style="color:'. $v .';">'. $k .'</p>';

        }

Edited by veedeoo: info added.

0

one more thing I forgot, I need to do this also, but for some reason is showing 0

echo "Totalni broj boja = " . array_sum($boje) . "\n";

 echo '<b> Boje po abecednom redu</b> <br/>';
    $boje = array('Crvena'=>'#FF0000','Narandzasta'=>'#FF7100', 'Zelena'=>'#00CC00', 'Plava'=>'#1435AD', 'Ljubicasta'=>'#A600A6', 'Zuta'=>'#FFE800', 'Svetlo plava'=>'#00FFCC', 'Bordo'=>'#990000');
    echo "Totalni broj boja = " . array_sum($boje) . "\n";
    ksort($boje);
    foreach($boje as $k => $v){
    echo '<p style="color:'. $v .';">'. $k .'</p>';
    }
2

I know it's solved, but it may be better to use 2 separate arrays - one for colours and one for text. That would give you the flexibility to use dynamic data.

BTW for anybody else, array_sum() is for adding numeric values in the array. count() is used to get the number of items in an array.

0

yes, I used count()

and its not important with what way I do it, because I stop here, it will be no fruther coding after this point, this is all I need to get as result, count array, and display it in different colors

thank you for your help

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.