Skip to content
Advertisement

How do i add an additional property to each element in array, how do i then use that property to affect the style of the output

How can i set a color to each object in array ? For example i want to set “Zen” (green) , “bless” (yellow).

$prices = array(
    " Zen " => $chk_prices['zen'],
    " Bless " => $chk_prices['bless'],
    " Credits " => $chk_prices['credit'], 
    " Chaos " => $chk_prices['chaos'], 
    " Creation " => $chk_prices['creation'],
    " Rena " => $chk_prices['rena'], 
    " Stone " => $chk_prices['stone'], 
    " Life " => $chk_prices['life'], 
    " Soul " => $chk_prices['soul']);

Can someone help me with this. Thanks.

Advertisement

Answer

looks like you need a multi dimensional array

$prices = array( “Zen” => [“price” => $chk_prices[‘zen’], “color” => “green”], and so on…. );

think of it like this

Zen is an array within an the $prices array (which now isn’t the best name for it as it contains other properties not just prices)

$prices = array( 
   "zen" => array(
        "price" => $chk_prices['zen'],
        "color" => "#7396FF"
    ),
    "Bless " => array(
        "price" => $chk_prices['bless'],
        "color" => "#7396FF>"
    )
)

to access the price of zen you would use $prices[‘zen’][‘price’]

a better way to do this would be to use objects, but that’s a whole other conversion.

i edited it again, because now i see you want to add styling.

you cant add styling to the output in this way. you need to echo some code using the values from the array.

for instance

echo '<span style="color:'.$prices['zen']['color'].';">zen price is '.$prices['zen']['price'].'</span>';

[i haven’t tested this code]

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement