从一个大的创建动态子数组

Create dynamic sub array from a big one

我有一个数组,是用爆炸和定界符 # 制作的。我试图从 PHP 子数组中的这个大数组创建但没有成功(子数组的数量不固定,可能会有所不同)。目标是使每 10 # 个新的子数组并存储 0 - 9、10 - 19 等之间的所有值...

这里是我的大数组,其中 PHP 必须处理:( ) 只是为了更清楚起见的注释,在代码中只有 #

#ACT/A (line 1)
#XXX (2)
#2 (3)
#51,6844 (4)
#50,7000 (5)
#101,40 (6)
#-1,97 (7)
#-1,91 (8)
#-0,61 (9)
#3,34 (10)

#ACT/B (11
#X
#4
#68,86750
#63,2700
#253,08
#-22,39
#-8,13
#-0,41
#8,27 (line 20)

#ACT/C
#X
#15
#10,33132
#4,18
#62,70
#-92,27
#-59,54
#0,00
#2,03

我的 PHP 代码(不起作用):

$start = 1;
$equities = explode("#", $allEquities); // split the big array here 

//$howManyEquities is a number between 1 and X, only int, not float)
while($start <= $howManyEquities) // doing the loop for each equities counted (could vary from 1 to x)
{
     $array[$howManyEquities] = $equities[0]; // trying to store in a array the result (line 1 from the example above, and then line 11, etc...)
     $equities = $equities[$start * 10]; // trying to prepare next loop, start (1) * 10 = 11 to catch next time result from line 11

     $start++;
}

综上所述,我可能不太清楚,请见谅。这是我想要的代码中的动态数组示例(我尝试了 foreach 循环但似乎没有用):

BigArray (the number of key inside vary according to the number of equity) = (
     subArray1 = (ACT/A, XXX, 2, 51,6844, etc from line 1 to 10)
     subArray2 = (ACT/B, X, 68,86750, etc from line 11 to 20)
     subArray3 = (ACT/C, etc)
     subArrayX = (ACT/X, etc)

它可以由第一个数组中每十个第一个值恢复,另一个数组中接下来的十个值,依此类推,直到我们覆盖所有大数组(这就是为什么我在我的代码中尝试 $start * 10 )。我必须准确地说,如果 $howManyEquities = 7,则将有 70 #,如果 = 5,则将有 50 #,依此类推。

编辑:解决方案感谢@user3783243

while($start <= $howManyEquities) {

$newArray = array_chunk($equities, 10);
$start++;

}

如果您需要更多信息,请不要犹豫,感谢阅读并享受周末! 恭敬

正如@user3783243 所说,array_chunk 完成了工作。

源字符串:

$string = '#ACT/A (line 1)
#XXX (2)
#2 (3)
#51,6844 (4)
#50,7000 (5)
#101,40 (6)
#-1,97 (7)
#-1,91 (8)
#-0,61 (9)
#3,34 (10)

#ACT/B (11
#X
#4
#68,86750
#63,2700
#253,08
#-22,39
#-8,13
#-0,41
#8,27 (line 20)

#ACT/C
#X
#15
#10,33132
#4,18
#62,70
#-92,27
#-59,54
#0,00
#2,03';

代码:

// Explode.
$array =  explode('#', $string);
// Should trim all values to f.e. remove new lines.
$array = array_map('trim', $array);
// Should filter empty values (due to empty lines in string).
$array = array_filter($array, 'strlen');
// Split into chunks.
$array = array_chunk($array, 10, true);

输出:

echo var_export($array, true) . PHP_EOL;
// [
//     0 => [
//         1  => 'ACT/A (line 1)',
//         2  => 'XXX (2)',
//         3  => '2 (3)',
//         4  => '51,6844 (4)',
//         5  => '50,7000 (5)',
//         6  => '101,40 (6)',
//         7  => '-1,97 (7)',
//         8  => '-1,91 (8)',
//         9  => '-0,61 (9)',
//         10 => '3,34 (10)',
//     ],
//     1 => [
//         11 => 'ACT/B (11',
//         12 => 'X',
//         13 => '4',
//         14 => '68,86750',
//         15 => '63,2700',
//         16 => '253,08',
//         17 => '-22,39',
//         18 => '-8,13',
//         19 => '-0,41',
//         20 => '8,27 (line 20)',
//     ],
//     2 => [
//         21 => 'ACT/C',
//         22 => 'X',
//         23 => '15',
//         24 => '10,33132',
//         25 => '4,18',
//         26 => '62,70',
//         27 => '-92,27',
//         28 => '-59,54',
//         29 => '0,00',
//         30 => '2,03',
//     ],
// ]