如何将命名键分配给数组? [复制]

Posted

技术标签:

【中文标题】如何将命名键分配给数组? [复制]【英文标题】:How do I assign named keys to arrays? [duplicate] 【发布时间】:2022-01-21 15:25:05 【问题描述】:

我正在尝试将一些 python 代码转换为 php,但我遇到了数组键的问题。

我的代码是这样的:

<?php
$speciesarray = array("Amanita aprica"=>"species/Amanita_aprica.html",
    "Amanita augusta"=>"species/Amanita_augusta.html",
    "Amanita calyptratoides"=>"species/Amanita_calyptratoides.html",
    "Amanita calyptroderma"=>"species/Amanita_calyptroderma.html",
    "Amanita constricta"=>"species/Amanita_constricta.html",
    "Amanita gemmata"=>"species/Amanita_gemmata.html",
    "Amanita magniverrucata"=>"species/Amanita_magniverrucata.html",
    "Amanita muscaria"=>"species/Amanita_muscaria.html",
    "Amanita novinupta"=>"species/Amanita_novinupta.html",
    "Amanita ocreata"=>"species/Amanita_ocreata.html",
    "Amanita pachycolea"=>"species/Amanita_pachycolea.html",
    "Amanita pantherina"=>"species/Amanita_pantherina.html",
    "Amanita phalloides"=>"species/Amanita_phalloides.html",
    "Amanita porphyria"=>"species/Amanita_porphyria.html",
    "Amanita protecta"=>"species/Amanita_protecta.html",
    "Amanita pruittii"=>"species/Amanita_pruittii.html",
    "Amanita silvicola"=>"species/Amanita_silvicola.html",
    "Amanita smithiana"=>"species/Amanita_smithiana.html",
    "Amanita vaginata"=>"species/Amanita_vaginata.html",
    "Amanita velosa"=>"species/Amanita_velosa.html",
    "Amanita vernicoccora"=>"species/Amanita_vernicoccora.html");

shuffle($speciesarray); // Randomize the species array
$speciesarray = array_slice($speciesarray, 0, 5); // Select 5 elements from the randomized species array
reset($speciesarray);
$choice = key($speciesarray); // Get the first key from the reduced array
shuffle($speciesarray); // Randomize again

/* For debugging */
print_r($speciesarray);
echo("<br/>")
print_r($choice);
?>

这是我期待看到的:

Array ( [Amanita silvicola] => species/Amanita_silvicola.html [Amanita gemmata] => species/Amanita_gemmata.html [Amanita calyptratoides] => species/Amanita_calyptratoides.html [Amanita vaginata] => species/Amanita_vaginata.html [Amanita phalloides] => species/Amanita_phalloides.html )
Amanita silvicola

这就是我实际得到的:

Array ( [0] => species/Amanita_silvicola.html [1] => species/Amanita_gemmata.html [2] => species/Amanita_calyptratoides.html [3] => species/Amanita_vaginata.html [4] => species/Amanita_phalloides.html )
0

我做错了什么?

【问题讨论】:

【参考方案1】:

来自php page on shuffle():

注意:此函数将新键分配给数组中的元素。它会 删除任何可能已分配的现有密钥,而不仅仅是 重新排序键。

该页面还提供了一个用户提供的功能,用于在随机播放中保留键:

<?php
    function shuffle_assoc(&$array) 
        $keys = array_keys($array);

        shuffle($keys);

        foreach($keys as $key) 
            $new[$key] = $array[$key];
        

        $array = $new;

        return true;
    
?>

在同一个 PHP 页面上还有许多其他用户为此场景贡献的函数

【讨论】:

【参考方案2】:

问题是,shuffle() 会杀死您的命名键并将您的数组更改为数字索引数组。因此,更好的解决方案是只打乱数组键,选择其中五个,然后选择与所选键关联的值:

$keys = array_keys( $speciesarray );
shuffle( $keys );
$keys = array_slice( $keys, 0, 5 );

$result = [];

foreach ( $keys as $key ) 
  $result[ $key ] = $speciesarray[ $key ];

【讨论】:

以上是关于如何将命名键分配给数组? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

将多个键分配给数组中的相同值

将数组从一行复制到一个数组对象中-“_numbersArray 永远不会分配给,并且始终具有其默认值 null”[关闭]

C语言如何给指针分配内存?

将二维数组分配为结构成员

将数组中的位置分配为反应键?

如何将现有数组值分配给数组指针?