PHP 数组 - 将每个元素的数字(值)附加到字符串(键)

PHP array - appending number (value) to string (key) for each element

提问人:machump 提问时间:10/17/2019 更新时间:10/17/2019 访问量:38

问:

我有一个数组,比如

$a = array
     (   
        "Leafs",  
        "Sabres",  
        "Red Wings",  
        "Flyers"
     ); 

我洗牌shuffle($a);

生成的数组可以是:

Array
(
    [0] => Sabres
    [1] => Red Wings
    [2] => Leafs
    [3] => Flyers
)

对于生成的数组,我现在想将一个序列号附加两次,从 开头 到 数组中的每个元素,这样就变成了:1$a

Array
(
    [1] => Sabres
    [1] => Red Wings
    [2] => Leafs
    [2] => Flyers
)

该数字在此数组中以结尾,但应继续,直到数组中没有更多要编号的元素。如何以编程方式完成此操作?2

PHP 数组 序列 随机播放

评论

6赞 splash58 10/17/2019
不能有没有唯一键的数组。所以是错误的。你真正的问题是什么?[1] => Sabres [1] => Red Wings
1赞 Teddy Patriarca 10/17/2019
数组索引必须是唯一的。你可以做一个多维数组array ( 1 => array( 'Sabres', 'Red Wings', ), 2 => array( 'Leafs',, 'Flyers' ) )
0赞 splash58 10/17/2019
@TeddyPatriarca 也许,OP只想使用array_chunk。或者,也许,其他一些东西。
0赞 machump 10/17/2019
@splash58可以设置为键和值?Sabres1
2赞 splash58 10/17/2019
array_chunk($array,2)

答: 暂无答案