Home > other >  Create dynamic sub array from a big one
Create dynamic sub array from a big one

Time:05-06

I have an array, made by using explode and the delimiter #. I'm trying without success to create from this big array in PHP subarrays (the number of subarrays is not fixed and could vary). The goal is to make every 10 # a new sub array and store in all the values between 0 - 9, 10 - 19, etc...

Here my big array where the PHP has to work on : the ( ) are just comment to be more clear, in the code there is only #

#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

My PHP code (which is not working) :

$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  ;
}

To sum up, I'm probably very not clear and I apologize. Here an example of the dynamic array I want from the code (I tried foreach loop but didn't seem to work) :

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)

It could be resumed by every ten first values inside a first array, the next ten in another array, and so on until we cover all the big array (that's why I tried $start * 10 in my code). I have to precise that if $howManyEquities = 7 by example, there will be 70 #, if = 5 there will be 50 # and so on.

EDIT : Solution thanks to @user3783243

while($start <= $howManyEquities) {

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

}

Don't hesitate if you need more information, thanks for reading and enjoy week-end ! Respectfully

CodePudding user response:

As @user3783243 said, array_chunk does the job.

Source string:

$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';

The code:

// 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);

Output:

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',
//     ],
// ]
  • Related