2

I have a hopefully quick question. I have placed an array in a constant but when I add array keys it craps out. My code below.

<?php define("my_const_arr", serialize(array("page_ids" => array("1234", "4123")))); ?>

<?php $page_id = "4123"; ?>

<?php if(in_array($page_id, unserialize(my_const_arr["page_ids"]))): ?>
<h1>Hello Stackoverflow</h1>
<?php endif; ?>
1
  • 1
    You should not be using constants like that. If you want to store an array, use a variable. Commented Apr 11, 2011 at 16:35

5 Answers 5

2

You can't do my_const_arr["page_ids"] at that point because it's still a string. You should unserialize it first and then access it

$arr = unserialize(my_const_arr);
if(in_array($page_id, $arr["page_ids"])):
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks JohnP! One more question if you can spare the time? Are there any downsides to doing this (i.e. How taxing are these methods really)? Khez seems to think I'm not using serialize/ unserialize correctly.... or PHP for that matter.
It depends. Unless those arrays are gigantic, switching them to anything else because of performance is premature micro-optimization. However, using a static class is a bit cleaner and encapsulates your array.
So would it be fair to saying that Heiko Bublitz's process is a much better fit in most applications/ scenarios?
@Smccullough nope, that method doesn't work. You basically write a class and hide your array in it. You could use a get only method to get at the variables in it. They don't need to be declared as constant since the class doesn't have a public setter. Or simply use a variable :)
2

You're using both unserialize and PHP mildly wrong:

<?php
define("my_const_arr", serialize(array("page_ids" => array("1234", "4123"))));
$page_id = "4123";
$a=unserialize(my_const_arr); // you need to usnerialize it before you can search for a specific key
if(in_array($page_id, $a["page_ids"])): ?>
<h1>Hello Stackoverflow</h1>
<?php endif; ?>

I would also like to point out that constants aren't particularly useful in an application you can control. Especially if that code is very relevant to your app.

<?php
$_myConstArr=array("page_ids" => array("1234", "4123"));

$page_id = "4123";
if(in_array($page_id, $_myConstArr["page_ids"])): ?>
<h1>Hello Stackoverflow</h1>
<?php endif; ?>

You will not get much overhead by doing this. I'd think that calling serialize/unserialize often would give you unwanted processing.

Post your exact scenario and a better solution might be made available.

Comments

1
<?php $arr = unserialize(my_const_arr) ?>    
<?php if(in_array($page_id, $arr["page_ids"])): ?>

Change it that way

Comments

1

'my_const_arr' is a constant, not an array.
So,my_const_arr["page_ids"] is incorrect.

Maybe you can try this:

$my_const_arr = unserialize(my_const_arr);
echo if(in_array($page_id,$my_const_arr)) 'HELLO STACKOVERFLOW' : '';

Comments

1

If there's no real need for the string-conversion, why not use a simple class as a container for constant values, like: EDIT: Sorry, just to leave a working approach:

Class MyConstants {
    public static $PAGE_IDS = array(1234, 4123);
}

Outside you can access it like

if (in_array( 4123, MyConstants::$PAGE_IDS )) {
    echo "got you! <br/>\n";
}

3 Comments

Thanks for your input Heiko :) I'll keep this one on the back burner for now.
Haha, no. This does't work. Constants can't hold arrays even if wrapped in classes.
-1 You can't actually declare arrays as constants like that. Please update

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.