I need array with 10 words,

then I need to sort that from a to z and display each in different color

can anyone help with that?

thanks in advance

Recommended Answers

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

Jump to Post

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>';

        }
Jump to Post

All 9 Replies

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

ye ok sorting part its easier, but loop thing, I never did it before

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";
}

?>

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>';

        }

that will do, thanks for your help

solved

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>';
    }

I solved this one too

Member Avatar

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.