Jump to content

Php Array Problem..


thunder708

Recommended Posts

Hi i have this code that turns hex value to RGB value but only problem is i can only find out how to print the array, i want to be able to use the array contents and im failing to do so here is the code

 


<?
function hex2rgb($hex){

$rgb = array();
$rgb['r'] = hexdec(substr($hex, 0, 2));
$rgb['g'] = hexdec(substr($hex, 2, 2));
$rgb['b'] = hexdec(substr($hex, 4, 2));
return $rgb;

}

print_r(hex2rgb('ffffff'));

?>

 

i have tried using:

 

echo "$rgb['r'];

 

and respectively for g and b but i cant get an output, any suggestions will be most helpful =D

 

thanks

Link to comment
Share on other sites

Try this

<?php
function hex2rgb($hex){

$rgb = array();
$rgb['r'] = hexdec(substr($hex, 0, 2));
$rgb['g'] = hexdec(substr($hex, 2, 2));
$rgb['b'] = hexdec(substr($hex, 4, 2));
return $rgb;

}

print_r(hex2rgb('ffffff'));

$colors = hex2rgb('ffffff');

echo "<br>".$colors['r'];

?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.