使用两个平面数组填充新二维数组的列[重复]

问题描述 投票:0回答:2

我想合并以下数组:

$songs = array(
    [0] => 001. Daddy's Groove Feat Mindshake - Surrender (Extended Mix).mp3
    [1] => 002. Haddaway - What Is Love (Project 46 Remix).mp3
    [2] => 003. Black Van - Fen Fires.mp3
    [3] => 004. Giorno - The Way (Radio Edit).mp3
    [4] => 005. Jack & Joy Vs Menini & Viani Ft Greg Stainer - #Aahm (Gran Hotel Mix).mp3
    [5] => 006. Almyron - Tonight (Radio Edit).mp3
    [6] => 007. Caminita Feat. Jan Peter - Tonite (Original Mix).mp3
    [7] => 008. Lucas Nord, Urban Cone, John Dahlback - We Were Gods (Original Mix).mp3
    [8] => 009. Luca Cassani - Greed (Extended Mix).mp3
    [9] => 010. Bakermat - Uitzicht.mp3
    [10] => 011. Freaky Behaviour - Dream Topping.mp3
)

$sizes = array(
    [0] => 14.1 MB
    [1] => 13.81 MB
    [2] => 12.5 MB
    [3] => 7.58 MB
    [4] => 14.15 MB
    [5] => 7.76 MB
    [6] => 10.85 MB
    [7] => 14.02 MB
    [8] => 12.66 MB
    [9] => 11.86 MB
    [10] => 17.57 MB
);

看起来像这样:

$result = array(
    [0] => array(
        name => 001. Daddy's Groove Feat Mindshake - Surrender (Extended Mix).mp3
        size => 14.1 MB
    )
    [1] => array(
        name => 002. Haddaway - What Is Love (Project 46 Remix).mp3
        size => 13.81 MB
    )
    ...
);

我尝试过

array_merge
array_combine
,但我做不到。我正在使用 PHP
5.5
$result
中的键与
$songs
$sizes
中的键相同并不重要。

php arrays multidimensional-array mapping grouping
2个回答
5
投票

您还可以使用array_map

$result = array_map(function ($song, $size) {
    return ['song' => $song, 'size' => $size];
}, $songs, $sizes);

1
投票

假设

$songs
$sizes
始终具有相同的长度,

$result = array();
for($i=0;$i<count($songs);$i++) {
    $result[] = array(
        'name'=>$songs[i],
        'size'=>$sizes[i]
    );
}
© www.soinside.com 2019 - 2024. All rights reserved.