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
https://forums.phpfreaks.com/topic/191338-php-array-problem/
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
https://forums.phpfreaks.com/topic/191338-php-array-problem/#findComment-1008792
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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