Is it possible to have an array assign its own element value to one of its other elements at the time you declare the array?
Here's a code example:
$arr = array ( 0 => '<ul class="'.$arr[1].'">', 1 => 'some_class' );
echo $arr[0]; // <ul class="some_class">
I've tried passing by reference (&$arr[1]
instead of $arr[1]
in the above code), but it gives a syntax error.
You can do something along these lines
$arr = array ( '<ul class="%s">', 'some_class' );
and when you want to render just call
echo call_user_func_array("sprintf", $arr);
No, you can't access an element of an array before the array has been declared. Just use another variable:
$class_name = 'some_class'
$arr = array ( 0 => '<ul class="'.$class_name.'">', 1 => $class_name );
echo $arr[0]; // <ul class="some_class">
You could also assign elements individually and refer to them in later assignments:
$arr = array();
$arr[1] = 'some_class';
$arr[0] = '<ul class="'.$arr[1].'">';
echo $arr[0]; // <ul class="some_class">