如何在单个数组中添加值(用于解析)

问题描述 投票:1回答:1

所以我问这个原因是因为我正在解析一些数据,然后将其放置在称为“卡”的数组中。如果我print_r($cards),我的输出将是这样:

[0] => The Hunt for Gollum
[1] => (x3)
[2] => The Hunt for Gollum
[3] => (x3)
[4] => The Hunt for Gollum
[5] => (x3)
[6] => The Hunt for Gollum
[7] => (x3)
[8] => The Hunt for Gollum
[9] => (x3)
[10] => The Hunt for Gollum
[11] => (x3)
[12] => The Hunt for Gollum
[13] => (x3)
[14] => The Hunt for Gollum
[15] => (x3)
[16] => The Hunt for Gollum
[17] => (x1)
[18] => The Hunt for Gollum
[19] => (x3)
[20] => The Hunt for Gollum
[21] => (x4)
[22] => The Hunt for Gollum
[23] => (x2/x0)
[24] => The Hunt for Gollum
[25] => (x3)
[26] => The Hunt for Gollum
[27] => (x2)
[28] => The Hunt for Gollum
[29] => (x2)
[30] => The Hunt for Gollum
[31] => (x2)
[32] => The Hunt for Gollum
[33] => (x2/x1)
[34] => The Hunt for Gollum
[35] => (x5/x2)
[36] => The Hunt for Gollum
[37] => (x2)
[38] => The Hunt for Gollum
[39] => (x2)
[40] => The Hunt for Gollum
[41] => (x3/x1)

[我问您是否知道一种方法,以便我可以像这样显示此数组:[0]=>The Hunt for Gollum (x3)[1]=>The Hunt for Gollum (x3)等...但是用更少的代码..不能使自己[47]倍于Thnx

php arrays
1个回答
1
投票

您创建了这样的新数组:

$array = ["The Hunt for Gollum", "(x2)","The Hunt for Gollum", "(x2)","The Hunt for Gollum", "(x2)"];
$newArray = [];
for( $i=0; $i < count($array) - 1; $i++ ){
    $newArray[] = $array[$i]. ' '. $array[$i+1];
    $i++;
}

print_r($newArray);

[C0在线试用

© www.soinside.com 2019 - 2024. All rights reserved.