数组中同一键中的多个值

时间:2014-08-31 02:11:03

标签: php arrays key

我是PHP的新手。但我试图在一个数组中的一个键中分配多个值,并且我遇到了问题。我做错了什么?

$skill_groups = array(
            'combat' => 'Attack', 'Defence', 'Strength', '', 'Ranged', 'Magic',
            'gathering' =>  'Mining', 'Woodcutting', 'Fishing', 'Hunter', 'Farming', 'Divination',
            'artisan' =>  'Smithing', 'Cooking', 'Runecrafting', 'Crafting', 'Firemaking', 'Herblore', 'Fletching', 'Construction',
            'support' => 'Dungeoneering', 'Thieving', 'Agility', 'Slayer', 'Prayer', 'Constitution'
);

1 个答案:

答案 0 :(得分:3)

您想要创建多维数组。正确的语法就像

$skill_groups = array(
        'combat' => array('Attack', 'Defence', 'Strength', '', 'Ranged', 'Magic'),
        'gathering' =>  array('Mining', 'Woodcutting', 'Fishing', 'Hunter', 'Farming', 'Divination'),
        'artisan' =>  array('Smithing', 'Cooking', 'Runecrafting', 'Crafting', 'Firemaking', 'Herblore', 'Fletching', 'Construction'),
        'support' => array('Dungeoneering', 'Thieving', 'Agility', 'Slayer', 'Prayer', 'Constitution'));

并且访问元素如下:

print_r($skill_groups['combat']); // something like array([0] => 'Attack', [1] => 'Defence', [2] => 'Strength', [3] => '', [4] => 'Ranged', [5] => 'Magic')
echo $skill_groups['combat'][0]; // 'Attack'