0

I have googled and used various approaches from this site, but somehow my problem isn't getting resolved.

Here is my problem: I have an array named $color and I want to add arrays into this (multidimensional) array, from within a function.

$color = array();

function hex2RGB($hex){
    $hex = ltrim($hex,'#');
    $a = hexdec(substr($hex,0,2));
    $b = hexdec(substr($hex,2,2));
    $c = hexdec(substr($hex,4,2));
    $rgb = array($a, $b, $c);
    array_push($color,$rgb);
}

hex2RGB("#97B92B");
hex2RGB("#D1422C");
hex2RGB("#66CAEA");

I know the function creates a good "rgb"-array with 3 values, I tested with screen output. But using array_push or $color[] = $rgb; doesn't add that array to the $color array. No error is shown, the "color"-array just stays empty.

4
  • can't you just have a simple array return on the end and assign it Commented Jul 22, 2016 at 0:06
  • 1
    Variable scope Commented Jul 22, 2016 at 0:11
  • Sidenote: this user contribution note shows a nice way to convert... Commented Jul 22, 2016 at 0:49
  • Thanks, FirstOne. sscanf seems like a handy function, didn't know of its existence. Commented Jul 22, 2016 at 23:27

3 Answers 3

1

You need to pass the $color array to the function by reference

function hex2RGB($hex, &$color){ // '&' means changes to $color will persist
   ...
}
$color = [];
hex2RGB('#...',$color);//after this line, $color will contain the data you want

I would favor this over using global within a function because with this approach, you control exactly which array gets modified (you pass it when calling the function). Using global can lead to unintended consequences if you forget when calling a function that it will change other variables in your scope. The better design is to keep your code modularized (just search for recommendations on the use of global vars to see for yourself).

Sign up to request clarification or add additional context in comments.

1 Comment

I was looking for this (an option without global) +1
0

You need to globalise your $color array to use it inside functions.

<?php
$color = array();

function hex2RGB($hex){
global $color; // magic happens here

$hex = ltrim($hex,'#');
$a = hexdec(substr($hex,0,2));
$b = hexdec(substr($hex,2,2));
$c = hexdec(substr($hex,4,2));
$rgb = array($a, $b, $c);
array_push($color,$rgb);
}

fixes your problem.

For more information read the Scope section of the php tutorial

Comments

0

See below. You need to allow the global variable $color to be used in the function by using the "global" keyword:

$color = array();

function hex2RGB($hex){
global $color; //<----------------this line here is needed to use $color
$hex = ltrim($hex,'#');
$a = hexdec(substr($hex,0,2));
$b = hexdec(substr($hex,2,2));
$c = hexdec(substr($hex,4,2));
$rgb = array($a, $b, $c);
array_push($color,$rgb);
}

hex2RGB("#97B92B");
hex2RGB("#D1422C");
hex2RGB("#66CAEA");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.