PHP使数组值等于同一数组中的另一个值

时间:2014-12-17 16:30:52

标签: php arrays

我想知道是否可以将数组键分配给另一个键的值,例如:

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

或类似的东西,但我只是得到关于PHP的错误,期待其他东西:

  

解析错误:语法错误,意外' $ TYPE_LOCALES' (T_VARIABLE),期待标识符(T_STRING)或类(T_CLASS)...

有问题的数组所在的类如下:

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]
    );
}

正如你所看到的,我正试图制作这些'密钥等于'默认'。但是我不想提取默认数组(在数组外定义它)然后只是在里面使用它,因为会有更多的数组,每个数组都等于任何其他数组。

更新

这是我已经尝试过的:

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]

没有运气。

1 个答案:

答案 0 :(得分: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);

输出:

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

关于你的其他问题:

在课堂上不可能这样做。我尝试使用静态,非静态,还有getter和setter,public和private。没有机会。不允许在之前未设置的类中分配值。至少,我认为没关系。

在这里你可以了解这是如何运作的:

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);

是的,我知道,那不是你想要的。但你甚至不需要尝试这个:

final class Example {

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

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

PHP只是不允许这是在一个类中。无论它是否静止。

你走了:

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());

结果:

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
        )

)