0

I wonder if it's possible to assign an array key to a value of another key, for example:

$array = array(
    'key1' => 'Foo'
    'key2' => $array['key1']
    );

Or something like that, but I'm just getting errors about PHP expecting other stuff:

Parse error: syntax error, unexpected '$TYPE_LOCALES' (T_VARIABLE), expecting identifier (T_STRING) or class (T_CLASS) in …

The class where the array in question is located is as follows:

final class UserTypes {
    const TYPE_ADMINISTRATOR = 1;
    const TYPE_CALL_CENTER_OP = 2;
    const TYPE_SALES_LEADER = 3;
    const TYPE_SALES_OP = 4;

    const TYPE_LOCALE_DEFAULT = 'default';
    const TYPE_LOCALE_ES = 'es';

    private static $TYPE_LOCALES = array(
        TYPE_LOCALE_DEFAULT => array(
            TYPE_ADMINISTRATOR => 'administrador',
            TYPE_CALL_CENTER_OP => 'agente de call center',
            TYPE_SALES_LEADER => 'partner líder',
            TYPE_SALES_OP => 'partner operador',
            ),
        TYPE_LOCALE_ES => self::$TYPE_LOCALES[TYPE_LOCALE_DEFAULT]
    );
}

And as you can see, I'm trying to make the 'es' key equal to 'default'. But I don't want to extract the default array (defining it outside the array) and then just using it inside, since there will be more arrays that will each be equal to any other array.

Update:

This is what I've tried already:

TYPE_LOCALE_ES => self::$TYPE_LOCALES[TYPE_LOCALE_DEFAULT]

TYPE_LOCALE_ES => UserTypes::$TYPE_LOCALES[TYPE_LOCALE_DEFAULT]

TYPE_LOCALE_ES => $TYPE_LOCALES[TYPE_LOCALE_DEFAULT]

TYPE_LOCALE_ES => &$TYPE_LOCALES[TYPE_LOCALE_DEFAULT]

TYPE_LOCALE_ES => &self::$TYPE_LOCALES[TYPE_LOCALE_DEFAULT]

TYPE_LOCALE_ES => self::&$TYPE_LOCALES[TYPE_LOCALE_DEFAULT]

TYPE_LOCALE_ES => &UserTypes::$TYPE_LOCALES[TYPE_LOCALE_DEFAULT]

TYPE_LOCALE_ES => UserTypes::&$TYPE_LOCALES[TYPE_LOCALE_DEFAULT]

Without luck.

6
  • 1
    how do you expect to be able to use $TYPE_LOCALE within the very block of code that's DEFINING it? PHP can't reach into the future to get values from that variable. Commented Dec 17, 2014 at 16:34
  • You are also missing a comma. Commented Dec 17, 2014 at 16:37
  • No coma mussing. @MarcB has right. If you change the row self::$TYPE_LOCALES[TYPÊ_LOCALE_DEFAULT] to 1, then it's syntactically right. Commented Dec 17, 2014 at 16:38
  • To 1? Is that an example? I know a constant will work but that's not what I'm trying to get. Commented Dec 17, 2014 at 16:40
  • constant values are not meant to be modified. they are constant for a reason. You would need something like $arr[TYPE_LOCALE_DEFAULT] => array() for instance. Commented Dec 17, 2014 at 16:40

1 Answer 1

4
<pre>
<?php
// This will not work, because key1 is not defined yet (I guess)
$array = array(
    'key1' => 'Foo',
    'key2' => $array['key1']
);

print_r($array);

// But if you define a reference, it will work
$array = array(
    'key1' => 'Foo',
    'key2' => &$array['key1']
);

print_r($array);

Output:

Array
(
    [key1] => Foo
    [key2] => 
)
Array
(
    [key1] => Foo
    [key2] => Foo
)

Concerning your other problem:

It is not possible to do that in a class. I tried with static, non static, also with getters and setters, public and private. No chance. It is not allowed to assign a value in a class that was not already set before. And at least, I think it's ok.

Here you get an idea, how this might work:

final class Example {

    public $array = array(
        'key1' => 'Foo',
        'key2' => ''
    );

    public function &getRefArray($key) {
        return $this->array[$key];
    }
}

$oExample = new Example();

// Not working:
//$oExample->array['key2'] = &$oExample->array('key1');

// Working:
$oExample->array['key2'] = &$oExample->getRefArray('key1');
print_r($oExample->array);

Yeah, I know, that is not what you want. But you don't even need to try this:

final class Example {

    public $array = array(
        'key1' => 'Foo',
        'key2' => &$this->getRefArray('key1');
    );

    public function &getRefArray($key) {
        return $this->array[$key];
    }
}

PHP just don't allow this is in a class. No matter if it is static or not.

Here you go:

class UserTypes {
    const TYPE_ADMINISTRATOR = 1;
    const TYPE_CALL_CENTER_OP = 2;
    const TYPE_SALES_LEADER = 3;
    const TYPE_SALES_OP = 4;

    const TYPE_LOCALE_DEFAULT = 'default';
    const TYPE_LOCALE_ES = 'es';
    const TYPE_LOCALE_DE = 'de';

    private static $TYPE_LOCALE_ES = array(
            TYPE_ADMINISTRATOR => 'administrador',
            TYPE_CALL_CENTER_OP => 'agente de call center',
            TYPE_SALES_LEADER => 'partner líder',
            TYPE_SALES_OP => 'partner operador',
            );

    private static $TYPE_LOCALE_DE = array(
            TYPE_ADMINISTRATOR => 'Administrator',
            TYPE_CALL_CENTER_OP => 'Callcenter Agent',
            TYPE_SALES_LEADER => 'Partner Vertriebsleiter',
            TYPE_SALES_OP => 'Partner Vertriebsmitarbeiter',
            );

    /* Won't work because it's in a static class:
    private static $TYPE_LOCALES = array(
            TYPE_LOCALE_DEFAULT => self::$TYPE_LOCALE_ES,
            TYPE_LOCALE_ES =>  self::$TYPE_LOCALE_ES,
            TYPE_LOCALE_DE =>  self::$TYPE_LOCALE_DE
        );
    */

    /* Won't work because it's in a static class:
    private static $TYPE_LOCALES = self::getLocalesArray();
    */

    public static function getLocalesArray() {
        return array(
            TYPE_LOCALE_DEFAULT => self::$TYPE_LOCALE_ES,
            TYPE_LOCALE_ES =>  self::$TYPE_LOCALE_ES,
            TYPE_LOCALE_DE =>  self::$TYPE_LOCALE_DE
        );
    }


}

print_r(UserTypes::getLocalesArray());

Result:

Array
(
    [TYPE_LOCALE_DEFAULT] => Array
        (
            [TYPE_ADMINISTRATOR] => administrador
            [TYPE_CALL_CENTER_OP] => agente de call center
            [TYPE_SALES_LEADER] => partner líder
            [TYPE_SALES_OP] => partner operador
        )

    [TYPE_LOCALE_ES] => Array
        (
            [TYPE_ADMINISTRATOR] => administrador
            [TYPE_CALL_CENTER_OP] => agente de call center
            [TYPE_SALES_LEADER] => partner líder
            [TYPE_SALES_OP] => partner operador
        )

    [TYPE_LOCALE_DE] => Array
        (
            [TYPE_ADMINISTRATOR] => Administrator
            [TYPE_CALL_CENTER_OP] => Callcenter Agent
            [TYPE_SALES_LEADER] => Partner Vertriebsleiter
            [TYPE_SALES_OP] => Partner Vertriebsmitarbeiter
        )

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

4 Comments

I have already tried a reference with &$TYPE_LOCALES[TYPE_LOCALE_DEFAULT] and even &self::$TYPE_LOCALES[TYPE_LOCALE_DEFAULT] or self::&$TYPE_LOCALES[TYPE_LOCALE_DEFAULT] but PHP now says: Parse error: syntax error, unexpected '&'…
It was a typo at copy-paste time, but it's now corrected (it is not in the original code).
And just a hint: If you want to use a constant defined in your class you've to use it like this: self::TYPE_ADMINISTRATOR. Otherwise this will be just strings if they are not creaded in gloabal context.
Thank you for your answer, what I did was ultimately make it fallback to default at runtime, kind of what you did with the getter. Thank you again!

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.