Forum Moderators: coopster

Message Too Old, No Replies

Defining an array

Or making it a constant

         

ahmedtheking

9:09 am on Jun 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How can I make an array a constant?

eelixduppy

12:48 pm on Jun 13, 2006 (gmt 0)



I think you can do this within a class with the following:
const fish = array();

But I'm not too sure. i would suggest trying different things. Other than this, I primarily set constants with define() [us2.php.net].

ahmedtheking

1:33 pm on Jun 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm, i tried define, didn't work!

eelixduppy

1:49 pm on Jun 13, 2006 (gmt 0)



I found this at php.net; it's one of the user-contributed notes. Here it is, I do not know if this works because I cannot test it at this time. Good luck!

My hack at constant arrays :

function carray($const = null, $index = null)
{
static $carrays;

if ($const === null)
{
$i = count($carrays);
$carrays[$i] = $index;

return $i;

} else
{
return $index === null? $carrays[$const] : $carrays[$const][$index];
}
}

function aconst()
{
$args = func_get_args();

if (count($args) == 1 && is_array($args[0]))
{
return carray(null, $args[0]);

} else
{
return carray(null, $args);
}
}

Use like this:

define('NRWORDS', aconst('zero', 'one', 'two', 'three'));

define('STUNT_DESC', aconst(array(
STUNT_TWOWHEELS => 'Two wheels',
STUNT_WHEELIE => 'Wheelie',
STUNT_STOPPIE => 'Stoppie'
)));

print(carray(NRWORDS, 2));

print(carray(STUNT_DESC, STUNT_WHEELIE));


$arr = carray(STUNT_DESC);

print_r($arr[STUNT_STOPPIE]);

bvr.

mcavic

5:46 pm on Jun 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think what you mean by constant is something like this:

define ("MAXSIZE", 100);
echo MAXSIZE;

I'm not sure if you can do that with an array. (If you could, it would probably add confusion). I would just use a variable array:

$arr = array("one", "two", "three");

And it should basically behave the same way. If you need it to be global, then do:

global $arr;

inside every function where you're using it.

ahmedtheking

12:01 pm on Jun 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah ok cool!

coopster

2:03 am on Jun 18, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



For the record:


Only scalar data (boolean, integer, float and string) can be contained in constants.

Constants [php.net]

ahmedtheking

1:57 pm on Jun 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, so prob best to implode it and then define?

mcavic

2:34 pm on Jun 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could define an imploded array, yes. But then you'd have to explode it later, so why not just use a variable array to begin with?

ahmedtheking

2:52 pm on Jun 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well it's just for coding sakes (eg so it doesn't get overwritten, it can be used everywhere etc...). Prob gonna stick with GLOBAL $array.