How to use recursive function that converts csv file to multidimensional arrayHow do I check if an array includes an object in JavaScript?How do you convert a byte array to a hexadecimal string, and vice versa?How to append something to an array?How to insert an item into an array at a specific index (JavaScript)?How do you check if a variable is an array in JavaScript?How do I determine whether an array contains a particular value in Java?How do I declare and initialize an array in Java?How do I empty an array in JavaScript?How to check if an object is an array?How do I remove a particular element from an array in JavaScript?

Ask for a paid taxi in order to arrive as early as possible for an interview within the city

How to avoid using System.String with Rfc2898DeriveBytes in C#

Is there a known non-euclidean geometry where two concentric circles of different radii can intersect? (as in the novel "The Universe Between")

Can you feel passing through the sound barrier in an F-16?

Why would the US President need briefings on UFOs?

Fancy String Replace

Factoring the square of this polynomial?

Is there a limit on how long the casting (speaking aloud part of the spell) of Wish can be?

Can pay be witheld for hours cleaning up after closing time?

What magic extends life or grants immortality?

If the first law of thermodynamics ensures conservation of energy, why does it allow systems to lose energy?

Is it safe to remove the bottom chords of a series of garage roof trusses?

What professions would a medieval village with a population of 100 need?

What is this symbol: semicircles facing eachother

If all stars rotate, why was there a theory developed, that requires non-rotating stars?

confused about grep and the * wildcard

Why is my Earth simulation slower than the reality?

Why did MS-DOS applications built using Turbo Pascal fail to start with a division by zero error on faster systems?

How does turbine efficiency compare with internal combustion engines if all the turbine power is converted to mechanical energy?

Is it appropriate for a prospective landlord to ask me for my credit report?

Why does my house heat up, even when it's cool outside?

How to compare two different formulations of a problem?

Church Booleans

Can I switch to third-person while not in 'town' in Destiny 2?



How to use recursive function that converts csv file to multidimensional array


How do I check if an array includes an object in JavaScript?How do you convert a byte array to a hexadecimal string, and vice versa?How to append something to an array?How to insert an item into an array at a specific index (JavaScript)?How do you check if a variable is an array in JavaScript?How do I determine whether an array contains a particular value in Java?How do I declare and initialize an array in Java?How do I empty an array in JavaScript?How to check if an object is an array?How do I remove a particular element from an array in JavaScript?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-1















i'm trying to figure out how to create a recursive function (PHP) that merges csv file into a multi dimensional array.



i got a csv file that looks like this



id,parent_id,date
1,0,2017
2,1,2017-01
3,1,2017-02
4,1,2017-03
5,1,2017-04
6,1,2017-05
7,1,2017-06
8,1,2017-07
9,1,2017-08
10,1,2017-09
11,1,2017-10
12,1,2017-11
13,1,2017-12
14,0,2018
15,14,2018-01
16,14,2018-02
17,14,2018-03
18,14,2018-04
19,14,2018-05
20,14,2018-06
21,14,2018-07
22,14,2018-08
23,14,2018-09
24,14,2018-10
25,14,2018-11
26,14,2018-12
27,0,2019
28,27,2019-01
29,27,2019-02
30,27,2019-03
31,27,2019-04
32,27,2019-05
33,27,2019-06
34,27,2019-07
35,27,2019-08
36,27,2019-09
37,27,2019-10
38,27,2019-11
39,27,2019-12


and the output should be like this -



Array
(
[1] => Array
(
[date] => 2017
[dates] => Array
(
[2] => 2017-01
[3] => 2017-02
[4] => 2017-03
[5] => 2017-04
[6] => 2017-05
[7] => 2017-06
[8] => 2017-07
[9] => 2017-08
[10] => 2017-09
[11] => 2017-10
[12] => 2017-11
[13] => 2017-12
)

)

[14] => Array
(
[date] => 2018
[dates] => Array
(
[15] => 2018-01
[16] => 2018-02
[17] => 2018-03
[18] => 2018-04
[19] => 2018-05
[20] => 2018-06
[21] => 2018-07
[22] => 2018-08
[23] => 2018-09
[24] => 2018-10
[25] => 2018-11
[26] => 2018-12
)

)

[27] => Array
(
[date] => 2019
[dates] => Array
(
[28] => 2019-01
[29] => 2019-02
[30] => 2019-03
[31] => 2019-04
[32] => 2019-05
[33] => 2019-06
[34] => 2019-07
[35] => 2019-08
[36] => 2019-09
[37] => 2019-10
[38] => 2019-11
[39] => 2019-12
)

)

)


i'm reading the csv file into an array, with this output -



Array(
[0] => Array(
[0] => id[1] => parent_id[2] => date
) [1] => Array(
[0] => 1[1] => 0[2] => 2017
) [2] => Array(
[0] => 2[1] => 1[2] => 2017 - 01
) [3] => Array(
[0] => 3[1] => 1[2] => 2017 - 02
) [4] => Array(
[0] => 4[1] => 1[2] => 2017 - 03
) [5] => Array(
[0] => 5[1] => 1[2] => 2017 - 04
) [6] => Array(
[0] => 6[1] => 1[2] => 2017 - 05
) [7] => Array(
[0] => 7[1] => 1[2] => 2017 - 06
) [8] => Array(
[0] => 8[1] => 1[2] => 2017 - 07
) [9] => Array(
[0] => 9[1] => 1[2] => 2017 - 08
) [10] => Array(
[0] => 10[1] => 1[2] => 2017 - 09
) [11] => Array(
[0] => 11[1] => 1[2] => 2017 - 10
) [12] => Array(
[0] => 12[1] => 1[2] => 2017 - 11
) [13] => Array(
[0] => 13[1] => 1[2] => 2017 - 12
) [14] => Array(
[0] => 14[1] => 0[2] => 2018
) [15] => Array(
[0] => 15[1] => 14[2] => 2018 - 01
) [16] => Array(
[0] => 16[1] => 14[2] => 2018 - 02
) [17] => Array(
[0] => 17[1] => 14[2] => 2018 - 03
) [18] => Array(
[0] => 18[1] => 14[2] => 2018 - 04
) [19] => Array(
[0] => 19[1] => 14[2] => 2018 - 05
) [20] => Array(
[0] => 20[1] => 14[2] => 2018 - 06
) [21] => Array(
[0] => 21[1] => 14[2] => 2018 - 07
) [22] => Array(
[0] => 22[1] => 14[2] => 2018 - 08
) [23] => Array(
[0] => 23[1] => 14[2] => 2018 - 09
) [24] => Array(
[0] => 24[1] => 14[2] => 2018 - 10
) [25] => Array(
[0] => 25[1] => 14[2] => 2018 - 11
) [26] => Array(
[0] => 26[1] => 14[2] => 2018 - 12
) [27] => Array(
[0] => 27[1] => 0[2] => 2019
) [28] => Array(
[0] => 28[1] => 27[2] => 2019 - 01
) [29] => Array(
[0] => 29[1] => 27[2] => 2019 - 02
) [30] => Array(
[0] => 30[1] => 27[2] => 2019 - 03
) [31] => Array(
[0] => 31[1] => 27[2] => 2019 - 04
) [32] => Array(
[0] => 32[1] => 27[2] => 2019 - 05
) [33] => Array(
[0] => 33[1] => 27[2] => 2019 - 06
) [34] => Array(
[0] => 34[1] => 27[2] => 2019 - 07
) [35] => Array(
[0] => 35[1] => 27[2] => 2019 - 08
) [36] => Array(
[0] => 36[1] => 27[2] => 2019 - 09
) [37] => Array(
[0] => 37[1] => 27[2] => 2019 - 10
) [38] => Array(
[0] => 38[1] => 27[2] => 2019 - 11
) [39] => Array(
[0] => 39[1] => 27[2] => 2019 - 12
)
)


and i'm trying to apply this array on this function that should get me the output that i wanted



function buildTree(array $elements, $parentId = 0) 
$branch = array();

foreach ($elements as $element)
if ($element['parent_id'] == $parentId)
$children = buildTree($elements, $element['id']);
if ($children)
$element['children'] = $children;

$branch[] = $element;



return $branch;


$tree = buildTree($MyArray);


but i just cant figure out how to apply the recursive function over here, and how to use the 'parent id' and 'id' elements in the foreach scope...
i'm really lost at this point.










share|improve this question


























  • I don't see any reason for recursive function.

    – u_mulder
    Mar 27 at 16:08











  • i see that, but this is what the lecturer asked for.. @u_mulder

    – Àtishking
    Mar 27 at 16:09

















-1















i'm trying to figure out how to create a recursive function (PHP) that merges csv file into a multi dimensional array.



i got a csv file that looks like this



id,parent_id,date
1,0,2017
2,1,2017-01
3,1,2017-02
4,1,2017-03
5,1,2017-04
6,1,2017-05
7,1,2017-06
8,1,2017-07
9,1,2017-08
10,1,2017-09
11,1,2017-10
12,1,2017-11
13,1,2017-12
14,0,2018
15,14,2018-01
16,14,2018-02
17,14,2018-03
18,14,2018-04
19,14,2018-05
20,14,2018-06
21,14,2018-07
22,14,2018-08
23,14,2018-09
24,14,2018-10
25,14,2018-11
26,14,2018-12
27,0,2019
28,27,2019-01
29,27,2019-02
30,27,2019-03
31,27,2019-04
32,27,2019-05
33,27,2019-06
34,27,2019-07
35,27,2019-08
36,27,2019-09
37,27,2019-10
38,27,2019-11
39,27,2019-12


and the output should be like this -



Array
(
[1] => Array
(
[date] => 2017
[dates] => Array
(
[2] => 2017-01
[3] => 2017-02
[4] => 2017-03
[5] => 2017-04
[6] => 2017-05
[7] => 2017-06
[8] => 2017-07
[9] => 2017-08
[10] => 2017-09
[11] => 2017-10
[12] => 2017-11
[13] => 2017-12
)

)

[14] => Array
(
[date] => 2018
[dates] => Array
(
[15] => 2018-01
[16] => 2018-02
[17] => 2018-03
[18] => 2018-04
[19] => 2018-05
[20] => 2018-06
[21] => 2018-07
[22] => 2018-08
[23] => 2018-09
[24] => 2018-10
[25] => 2018-11
[26] => 2018-12
)

)

[27] => Array
(
[date] => 2019
[dates] => Array
(
[28] => 2019-01
[29] => 2019-02
[30] => 2019-03
[31] => 2019-04
[32] => 2019-05
[33] => 2019-06
[34] => 2019-07
[35] => 2019-08
[36] => 2019-09
[37] => 2019-10
[38] => 2019-11
[39] => 2019-12
)

)

)


i'm reading the csv file into an array, with this output -



Array(
[0] => Array(
[0] => id[1] => parent_id[2] => date
) [1] => Array(
[0] => 1[1] => 0[2] => 2017
) [2] => Array(
[0] => 2[1] => 1[2] => 2017 - 01
) [3] => Array(
[0] => 3[1] => 1[2] => 2017 - 02
) [4] => Array(
[0] => 4[1] => 1[2] => 2017 - 03
) [5] => Array(
[0] => 5[1] => 1[2] => 2017 - 04
) [6] => Array(
[0] => 6[1] => 1[2] => 2017 - 05
) [7] => Array(
[0] => 7[1] => 1[2] => 2017 - 06
) [8] => Array(
[0] => 8[1] => 1[2] => 2017 - 07
) [9] => Array(
[0] => 9[1] => 1[2] => 2017 - 08
) [10] => Array(
[0] => 10[1] => 1[2] => 2017 - 09
) [11] => Array(
[0] => 11[1] => 1[2] => 2017 - 10
) [12] => Array(
[0] => 12[1] => 1[2] => 2017 - 11
) [13] => Array(
[0] => 13[1] => 1[2] => 2017 - 12
) [14] => Array(
[0] => 14[1] => 0[2] => 2018
) [15] => Array(
[0] => 15[1] => 14[2] => 2018 - 01
) [16] => Array(
[0] => 16[1] => 14[2] => 2018 - 02
) [17] => Array(
[0] => 17[1] => 14[2] => 2018 - 03
) [18] => Array(
[0] => 18[1] => 14[2] => 2018 - 04
) [19] => Array(
[0] => 19[1] => 14[2] => 2018 - 05
) [20] => Array(
[0] => 20[1] => 14[2] => 2018 - 06
) [21] => Array(
[0] => 21[1] => 14[2] => 2018 - 07
) [22] => Array(
[0] => 22[1] => 14[2] => 2018 - 08
) [23] => Array(
[0] => 23[1] => 14[2] => 2018 - 09
) [24] => Array(
[0] => 24[1] => 14[2] => 2018 - 10
) [25] => Array(
[0] => 25[1] => 14[2] => 2018 - 11
) [26] => Array(
[0] => 26[1] => 14[2] => 2018 - 12
) [27] => Array(
[0] => 27[1] => 0[2] => 2019
) [28] => Array(
[0] => 28[1] => 27[2] => 2019 - 01
) [29] => Array(
[0] => 29[1] => 27[2] => 2019 - 02
) [30] => Array(
[0] => 30[1] => 27[2] => 2019 - 03
) [31] => Array(
[0] => 31[1] => 27[2] => 2019 - 04
) [32] => Array(
[0] => 32[1] => 27[2] => 2019 - 05
) [33] => Array(
[0] => 33[1] => 27[2] => 2019 - 06
) [34] => Array(
[0] => 34[1] => 27[2] => 2019 - 07
) [35] => Array(
[0] => 35[1] => 27[2] => 2019 - 08
) [36] => Array(
[0] => 36[1] => 27[2] => 2019 - 09
) [37] => Array(
[0] => 37[1] => 27[2] => 2019 - 10
) [38] => Array(
[0] => 38[1] => 27[2] => 2019 - 11
) [39] => Array(
[0] => 39[1] => 27[2] => 2019 - 12
)
)


and i'm trying to apply this array on this function that should get me the output that i wanted



function buildTree(array $elements, $parentId = 0) 
$branch = array();

foreach ($elements as $element)
if ($element['parent_id'] == $parentId)
$children = buildTree($elements, $element['id']);
if ($children)
$element['children'] = $children;

$branch[] = $element;



return $branch;


$tree = buildTree($MyArray);


but i just cant figure out how to apply the recursive function over here, and how to use the 'parent id' and 'id' elements in the foreach scope...
i'm really lost at this point.










share|improve this question


























  • I don't see any reason for recursive function.

    – u_mulder
    Mar 27 at 16:08











  • i see that, but this is what the lecturer asked for.. @u_mulder

    – Àtishking
    Mar 27 at 16:09













-1












-1








-1








i'm trying to figure out how to create a recursive function (PHP) that merges csv file into a multi dimensional array.



i got a csv file that looks like this



id,parent_id,date
1,0,2017
2,1,2017-01
3,1,2017-02
4,1,2017-03
5,1,2017-04
6,1,2017-05
7,1,2017-06
8,1,2017-07
9,1,2017-08
10,1,2017-09
11,1,2017-10
12,1,2017-11
13,1,2017-12
14,0,2018
15,14,2018-01
16,14,2018-02
17,14,2018-03
18,14,2018-04
19,14,2018-05
20,14,2018-06
21,14,2018-07
22,14,2018-08
23,14,2018-09
24,14,2018-10
25,14,2018-11
26,14,2018-12
27,0,2019
28,27,2019-01
29,27,2019-02
30,27,2019-03
31,27,2019-04
32,27,2019-05
33,27,2019-06
34,27,2019-07
35,27,2019-08
36,27,2019-09
37,27,2019-10
38,27,2019-11
39,27,2019-12


and the output should be like this -



Array
(
[1] => Array
(
[date] => 2017
[dates] => Array
(
[2] => 2017-01
[3] => 2017-02
[4] => 2017-03
[5] => 2017-04
[6] => 2017-05
[7] => 2017-06
[8] => 2017-07
[9] => 2017-08
[10] => 2017-09
[11] => 2017-10
[12] => 2017-11
[13] => 2017-12
)

)

[14] => Array
(
[date] => 2018
[dates] => Array
(
[15] => 2018-01
[16] => 2018-02
[17] => 2018-03
[18] => 2018-04
[19] => 2018-05
[20] => 2018-06
[21] => 2018-07
[22] => 2018-08
[23] => 2018-09
[24] => 2018-10
[25] => 2018-11
[26] => 2018-12
)

)

[27] => Array
(
[date] => 2019
[dates] => Array
(
[28] => 2019-01
[29] => 2019-02
[30] => 2019-03
[31] => 2019-04
[32] => 2019-05
[33] => 2019-06
[34] => 2019-07
[35] => 2019-08
[36] => 2019-09
[37] => 2019-10
[38] => 2019-11
[39] => 2019-12
)

)

)


i'm reading the csv file into an array, with this output -



Array(
[0] => Array(
[0] => id[1] => parent_id[2] => date
) [1] => Array(
[0] => 1[1] => 0[2] => 2017
) [2] => Array(
[0] => 2[1] => 1[2] => 2017 - 01
) [3] => Array(
[0] => 3[1] => 1[2] => 2017 - 02
) [4] => Array(
[0] => 4[1] => 1[2] => 2017 - 03
) [5] => Array(
[0] => 5[1] => 1[2] => 2017 - 04
) [6] => Array(
[0] => 6[1] => 1[2] => 2017 - 05
) [7] => Array(
[0] => 7[1] => 1[2] => 2017 - 06
) [8] => Array(
[0] => 8[1] => 1[2] => 2017 - 07
) [9] => Array(
[0] => 9[1] => 1[2] => 2017 - 08
) [10] => Array(
[0] => 10[1] => 1[2] => 2017 - 09
) [11] => Array(
[0] => 11[1] => 1[2] => 2017 - 10
) [12] => Array(
[0] => 12[1] => 1[2] => 2017 - 11
) [13] => Array(
[0] => 13[1] => 1[2] => 2017 - 12
) [14] => Array(
[0] => 14[1] => 0[2] => 2018
) [15] => Array(
[0] => 15[1] => 14[2] => 2018 - 01
) [16] => Array(
[0] => 16[1] => 14[2] => 2018 - 02
) [17] => Array(
[0] => 17[1] => 14[2] => 2018 - 03
) [18] => Array(
[0] => 18[1] => 14[2] => 2018 - 04
) [19] => Array(
[0] => 19[1] => 14[2] => 2018 - 05
) [20] => Array(
[0] => 20[1] => 14[2] => 2018 - 06
) [21] => Array(
[0] => 21[1] => 14[2] => 2018 - 07
) [22] => Array(
[0] => 22[1] => 14[2] => 2018 - 08
) [23] => Array(
[0] => 23[1] => 14[2] => 2018 - 09
) [24] => Array(
[0] => 24[1] => 14[2] => 2018 - 10
) [25] => Array(
[0] => 25[1] => 14[2] => 2018 - 11
) [26] => Array(
[0] => 26[1] => 14[2] => 2018 - 12
) [27] => Array(
[0] => 27[1] => 0[2] => 2019
) [28] => Array(
[0] => 28[1] => 27[2] => 2019 - 01
) [29] => Array(
[0] => 29[1] => 27[2] => 2019 - 02
) [30] => Array(
[0] => 30[1] => 27[2] => 2019 - 03
) [31] => Array(
[0] => 31[1] => 27[2] => 2019 - 04
) [32] => Array(
[0] => 32[1] => 27[2] => 2019 - 05
) [33] => Array(
[0] => 33[1] => 27[2] => 2019 - 06
) [34] => Array(
[0] => 34[1] => 27[2] => 2019 - 07
) [35] => Array(
[0] => 35[1] => 27[2] => 2019 - 08
) [36] => Array(
[0] => 36[1] => 27[2] => 2019 - 09
) [37] => Array(
[0] => 37[1] => 27[2] => 2019 - 10
) [38] => Array(
[0] => 38[1] => 27[2] => 2019 - 11
) [39] => Array(
[0] => 39[1] => 27[2] => 2019 - 12
)
)


and i'm trying to apply this array on this function that should get me the output that i wanted



function buildTree(array $elements, $parentId = 0) 
$branch = array();

foreach ($elements as $element)
if ($element['parent_id'] == $parentId)
$children = buildTree($elements, $element['id']);
if ($children)
$element['children'] = $children;

$branch[] = $element;



return $branch;


$tree = buildTree($MyArray);


but i just cant figure out how to apply the recursive function over here, and how to use the 'parent id' and 'id' elements in the foreach scope...
i'm really lost at this point.










share|improve this question
















i'm trying to figure out how to create a recursive function (PHP) that merges csv file into a multi dimensional array.



i got a csv file that looks like this



id,parent_id,date
1,0,2017
2,1,2017-01
3,1,2017-02
4,1,2017-03
5,1,2017-04
6,1,2017-05
7,1,2017-06
8,1,2017-07
9,1,2017-08
10,1,2017-09
11,1,2017-10
12,1,2017-11
13,1,2017-12
14,0,2018
15,14,2018-01
16,14,2018-02
17,14,2018-03
18,14,2018-04
19,14,2018-05
20,14,2018-06
21,14,2018-07
22,14,2018-08
23,14,2018-09
24,14,2018-10
25,14,2018-11
26,14,2018-12
27,0,2019
28,27,2019-01
29,27,2019-02
30,27,2019-03
31,27,2019-04
32,27,2019-05
33,27,2019-06
34,27,2019-07
35,27,2019-08
36,27,2019-09
37,27,2019-10
38,27,2019-11
39,27,2019-12


and the output should be like this -



Array
(
[1] => Array
(
[date] => 2017
[dates] => Array
(
[2] => 2017-01
[3] => 2017-02
[4] => 2017-03
[5] => 2017-04
[6] => 2017-05
[7] => 2017-06
[8] => 2017-07
[9] => 2017-08
[10] => 2017-09
[11] => 2017-10
[12] => 2017-11
[13] => 2017-12
)

)

[14] => Array
(
[date] => 2018
[dates] => Array
(
[15] => 2018-01
[16] => 2018-02
[17] => 2018-03
[18] => 2018-04
[19] => 2018-05
[20] => 2018-06
[21] => 2018-07
[22] => 2018-08
[23] => 2018-09
[24] => 2018-10
[25] => 2018-11
[26] => 2018-12
)

)

[27] => Array
(
[date] => 2019
[dates] => Array
(
[28] => 2019-01
[29] => 2019-02
[30] => 2019-03
[31] => 2019-04
[32] => 2019-05
[33] => 2019-06
[34] => 2019-07
[35] => 2019-08
[36] => 2019-09
[37] => 2019-10
[38] => 2019-11
[39] => 2019-12
)

)

)


i'm reading the csv file into an array, with this output -



Array(
[0] => Array(
[0] => id[1] => parent_id[2] => date
) [1] => Array(
[0] => 1[1] => 0[2] => 2017
) [2] => Array(
[0] => 2[1] => 1[2] => 2017 - 01
) [3] => Array(
[0] => 3[1] => 1[2] => 2017 - 02
) [4] => Array(
[0] => 4[1] => 1[2] => 2017 - 03
) [5] => Array(
[0] => 5[1] => 1[2] => 2017 - 04
) [6] => Array(
[0] => 6[1] => 1[2] => 2017 - 05
) [7] => Array(
[0] => 7[1] => 1[2] => 2017 - 06
) [8] => Array(
[0] => 8[1] => 1[2] => 2017 - 07
) [9] => Array(
[0] => 9[1] => 1[2] => 2017 - 08
) [10] => Array(
[0] => 10[1] => 1[2] => 2017 - 09
) [11] => Array(
[0] => 11[1] => 1[2] => 2017 - 10
) [12] => Array(
[0] => 12[1] => 1[2] => 2017 - 11
) [13] => Array(
[0] => 13[1] => 1[2] => 2017 - 12
) [14] => Array(
[0] => 14[1] => 0[2] => 2018
) [15] => Array(
[0] => 15[1] => 14[2] => 2018 - 01
) [16] => Array(
[0] => 16[1] => 14[2] => 2018 - 02
) [17] => Array(
[0] => 17[1] => 14[2] => 2018 - 03
) [18] => Array(
[0] => 18[1] => 14[2] => 2018 - 04
) [19] => Array(
[0] => 19[1] => 14[2] => 2018 - 05
) [20] => Array(
[0] => 20[1] => 14[2] => 2018 - 06
) [21] => Array(
[0] => 21[1] => 14[2] => 2018 - 07
) [22] => Array(
[0] => 22[1] => 14[2] => 2018 - 08
) [23] => Array(
[0] => 23[1] => 14[2] => 2018 - 09
) [24] => Array(
[0] => 24[1] => 14[2] => 2018 - 10
) [25] => Array(
[0] => 25[1] => 14[2] => 2018 - 11
) [26] => Array(
[0] => 26[1] => 14[2] => 2018 - 12
) [27] => Array(
[0] => 27[1] => 0[2] => 2019
) [28] => Array(
[0] => 28[1] => 27[2] => 2019 - 01
) [29] => Array(
[0] => 29[1] => 27[2] => 2019 - 02
) [30] => Array(
[0] => 30[1] => 27[2] => 2019 - 03
) [31] => Array(
[0] => 31[1] => 27[2] => 2019 - 04
) [32] => Array(
[0] => 32[1] => 27[2] => 2019 - 05
) [33] => Array(
[0] => 33[1] => 27[2] => 2019 - 06
) [34] => Array(
[0] => 34[1] => 27[2] => 2019 - 07
) [35] => Array(
[0] => 35[1] => 27[2] => 2019 - 08
) [36] => Array(
[0] => 36[1] => 27[2] => 2019 - 09
) [37] => Array(
[0] => 37[1] => 27[2] => 2019 - 10
) [38] => Array(
[0] => 38[1] => 27[2] => 2019 - 11
) [39] => Array(
[0] => 39[1] => 27[2] => 2019 - 12
)
)


and i'm trying to apply this array on this function that should get me the output that i wanted



function buildTree(array $elements, $parentId = 0) 
$branch = array();

foreach ($elements as $element)
if ($element['parent_id'] == $parentId)
$children = buildTree($elements, $element['id']);
if ($children)
$element['children'] = $children;

$branch[] = $element;



return $branch;


$tree = buildTree($MyArray);


but i just cant figure out how to apply the recursive function over here, and how to use the 'parent id' and 'id' elements in the foreach scope...
i'm really lost at this point.







php arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 8:36







Àtishking

















asked Mar 27 at 16:06









ÀtishkingÀtishking

216 bronze badges




216 bronze badges















  • I don't see any reason for recursive function.

    – u_mulder
    Mar 27 at 16:08











  • i see that, but this is what the lecturer asked for.. @u_mulder

    – Àtishking
    Mar 27 at 16:09

















  • I don't see any reason for recursive function.

    – u_mulder
    Mar 27 at 16:08











  • i see that, but this is what the lecturer asked for.. @u_mulder

    – Àtishking
    Mar 27 at 16:09
















I don't see any reason for recursive function.

– u_mulder
Mar 27 at 16:08





I don't see any reason for recursive function.

– u_mulder
Mar 27 at 16:08













i see that, but this is what the lecturer asked for.. @u_mulder

– Àtishking
Mar 27 at 16:09





i see that, but this is what the lecturer asked for.. @u_mulder

– Àtishking
Mar 27 at 16:09












1 Answer
1






active

oldest

votes


















0













Every for loop can be converted into recursive function. You can create the following recursion function:



function setElement($res, $arr) 
if (count($arr) == 0)
return $res;
$e = explode(",",array_shift($arr));
if (isset($res[$e[1]]))
$res[$e[1]]["dates"][$e[0]] = $e[2]; // if exist add it
else $res[$e[0]] = array("date" => $e[2], "dates" => []); // not existed yet - so init it
return setElement($res, $arr);



Then use it with (example):



$arr = ["1,0,2017", "2,1,2017-01", "3,1,2017-02", "14,0,2018", "15,14,2018-01", "16,14,2018-02"];
$res = setElement([], $arr);


Now $res will contain your desire output.



Edit:



If your data comes from csv file you need to load them first. You may use fgetcsv as the follow function:



function getCSVarray($path) 
if (($handle = fopen($path, "r")) !== FALSE)
$keys = fgetcsv($handle, 1000, ",");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
$res[] = array_combine($keys, $data);

return $res;




Now use as:



$res = setElement([], getCSVarray("array.csv"));


Notice to modify setElement as now the indexes changed!



As:



function setElement($res, $arr) 
if (count($arr) == 0)
return $res;
$e = array_shift($arr);
if (isset($res[$e["parent_id"]]))
$res[$e["parent_id"]]["dates"][$e["id"]] = $e["date"]; // if exist add it
else $res[$e["id]] = array("date" => $e["date"], "dates" => []); // not existed yet - so init it
return setElement($res, $arr);



Edit 2:



Using your array ($MyArray) you can use:



function setElement($res, $arr) 
if (count($arr) == 0)
return $res;
$e = array_shift($arr);
if (isset($res[$e[1]]))
$res[$e[1]]["dates"][$e[0]] = $e[2]; // if exist add it
else $res[$e[0]] = array("date" => $e[2], "dates" => []); // not existed yet - so init it
return setElement($res, $arr);


//using it as:
array_shift($MyArray); // extract first element as it is the keys
$res = setElement([], $MyArray);





share|improve this answer



























  • i am trying to use you're example (which looks great) and i'm using the array like this - $arr = 'array.csv' (the csv file that contains the list, they are in the same folder of course) , and then using this $arr var like you used it, but it is not working for me... any idea why?

    – Àtishking
    Mar 28 at 6:24












  • @Àtishking edit my answer with reading the csv first

    – dWinder
    Mar 28 at 6:34











  • i'm getting this error - Warning: explode() expects parameter 2 to be string, array given in D:ComputershtdocsProjectarray.php this line - $e = explode(",",array_shift($arr));

    – Àtishking
    Mar 28 at 8:49












  • @Àtishking as I said - you need modification - updated my answer accordingly

    – dWinder
    Mar 28 at 9:00











  • first of all - i want to thank you for your patience, this is so not taken for granted. second of all, im using $res = setElement([], $MyArray); (MyArray is the array i loaded from the csv file) and than your function - and it is - still not working. i am getting error of " Undefined index id / parent id / date and so..."

    – Àtishking
    Mar 28 at 9:16











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55381725%2fhow-to-use-recursive-function-that-converts-csv-file-to-multidimensional-array%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0













Every for loop can be converted into recursive function. You can create the following recursion function:



function setElement($res, $arr) 
if (count($arr) == 0)
return $res;
$e = explode(",",array_shift($arr));
if (isset($res[$e[1]]))
$res[$e[1]]["dates"][$e[0]] = $e[2]; // if exist add it
else $res[$e[0]] = array("date" => $e[2], "dates" => []); // not existed yet - so init it
return setElement($res, $arr);



Then use it with (example):



$arr = ["1,0,2017", "2,1,2017-01", "3,1,2017-02", "14,0,2018", "15,14,2018-01", "16,14,2018-02"];
$res = setElement([], $arr);


Now $res will contain your desire output.



Edit:



If your data comes from csv file you need to load them first. You may use fgetcsv as the follow function:



function getCSVarray($path) 
if (($handle = fopen($path, "r")) !== FALSE)
$keys = fgetcsv($handle, 1000, ",");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
$res[] = array_combine($keys, $data);

return $res;




Now use as:



$res = setElement([], getCSVarray("array.csv"));


Notice to modify setElement as now the indexes changed!



As:



function setElement($res, $arr) 
if (count($arr) == 0)
return $res;
$e = array_shift($arr);
if (isset($res[$e["parent_id"]]))
$res[$e["parent_id"]]["dates"][$e["id"]] = $e["date"]; // if exist add it
else $res[$e["id]] = array("date" => $e["date"], "dates" => []); // not existed yet - so init it
return setElement($res, $arr);



Edit 2:



Using your array ($MyArray) you can use:



function setElement($res, $arr) 
if (count($arr) == 0)
return $res;
$e = array_shift($arr);
if (isset($res[$e[1]]))
$res[$e[1]]["dates"][$e[0]] = $e[2]; // if exist add it
else $res[$e[0]] = array("date" => $e[2], "dates" => []); // not existed yet - so init it
return setElement($res, $arr);


//using it as:
array_shift($MyArray); // extract first element as it is the keys
$res = setElement([], $MyArray);





share|improve this answer



























  • i am trying to use you're example (which looks great) and i'm using the array like this - $arr = 'array.csv' (the csv file that contains the list, they are in the same folder of course) , and then using this $arr var like you used it, but it is not working for me... any idea why?

    – Àtishking
    Mar 28 at 6:24












  • @Àtishking edit my answer with reading the csv first

    – dWinder
    Mar 28 at 6:34











  • i'm getting this error - Warning: explode() expects parameter 2 to be string, array given in D:ComputershtdocsProjectarray.php this line - $e = explode(",",array_shift($arr));

    – Àtishking
    Mar 28 at 8:49












  • @Àtishking as I said - you need modification - updated my answer accordingly

    – dWinder
    Mar 28 at 9:00











  • first of all - i want to thank you for your patience, this is so not taken for granted. second of all, im using $res = setElement([], $MyArray); (MyArray is the array i loaded from the csv file) and than your function - and it is - still not working. i am getting error of " Undefined index id / parent id / date and so..."

    – Àtishking
    Mar 28 at 9:16
















0













Every for loop can be converted into recursive function. You can create the following recursion function:



function setElement($res, $arr) 
if (count($arr) == 0)
return $res;
$e = explode(",",array_shift($arr));
if (isset($res[$e[1]]))
$res[$e[1]]["dates"][$e[0]] = $e[2]; // if exist add it
else $res[$e[0]] = array("date" => $e[2], "dates" => []); // not existed yet - so init it
return setElement($res, $arr);



Then use it with (example):



$arr = ["1,0,2017", "2,1,2017-01", "3,1,2017-02", "14,0,2018", "15,14,2018-01", "16,14,2018-02"];
$res = setElement([], $arr);


Now $res will contain your desire output.



Edit:



If your data comes from csv file you need to load them first. You may use fgetcsv as the follow function:



function getCSVarray($path) 
if (($handle = fopen($path, "r")) !== FALSE)
$keys = fgetcsv($handle, 1000, ",");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
$res[] = array_combine($keys, $data);

return $res;




Now use as:



$res = setElement([], getCSVarray("array.csv"));


Notice to modify setElement as now the indexes changed!



As:



function setElement($res, $arr) 
if (count($arr) == 0)
return $res;
$e = array_shift($arr);
if (isset($res[$e["parent_id"]]))
$res[$e["parent_id"]]["dates"][$e["id"]] = $e["date"]; // if exist add it
else $res[$e["id]] = array("date" => $e["date"], "dates" => []); // not existed yet - so init it
return setElement($res, $arr);



Edit 2:



Using your array ($MyArray) you can use:



function setElement($res, $arr) 
if (count($arr) == 0)
return $res;
$e = array_shift($arr);
if (isset($res[$e[1]]))
$res[$e[1]]["dates"][$e[0]] = $e[2]; // if exist add it
else $res[$e[0]] = array("date" => $e[2], "dates" => []); // not existed yet - so init it
return setElement($res, $arr);


//using it as:
array_shift($MyArray); // extract first element as it is the keys
$res = setElement([], $MyArray);





share|improve this answer



























  • i am trying to use you're example (which looks great) and i'm using the array like this - $arr = 'array.csv' (the csv file that contains the list, they are in the same folder of course) , and then using this $arr var like you used it, but it is not working for me... any idea why?

    – Àtishking
    Mar 28 at 6:24












  • @Àtishking edit my answer with reading the csv first

    – dWinder
    Mar 28 at 6:34











  • i'm getting this error - Warning: explode() expects parameter 2 to be string, array given in D:ComputershtdocsProjectarray.php this line - $e = explode(",",array_shift($arr));

    – Àtishking
    Mar 28 at 8:49












  • @Àtishking as I said - you need modification - updated my answer accordingly

    – dWinder
    Mar 28 at 9:00











  • first of all - i want to thank you for your patience, this is so not taken for granted. second of all, im using $res = setElement([], $MyArray); (MyArray is the array i loaded from the csv file) and than your function - and it is - still not working. i am getting error of " Undefined index id / parent id / date and so..."

    – Àtishking
    Mar 28 at 9:16














0












0








0







Every for loop can be converted into recursive function. You can create the following recursion function:



function setElement($res, $arr) 
if (count($arr) == 0)
return $res;
$e = explode(",",array_shift($arr));
if (isset($res[$e[1]]))
$res[$e[1]]["dates"][$e[0]] = $e[2]; // if exist add it
else $res[$e[0]] = array("date" => $e[2], "dates" => []); // not existed yet - so init it
return setElement($res, $arr);



Then use it with (example):



$arr = ["1,0,2017", "2,1,2017-01", "3,1,2017-02", "14,0,2018", "15,14,2018-01", "16,14,2018-02"];
$res = setElement([], $arr);


Now $res will contain your desire output.



Edit:



If your data comes from csv file you need to load them first. You may use fgetcsv as the follow function:



function getCSVarray($path) 
if (($handle = fopen($path, "r")) !== FALSE)
$keys = fgetcsv($handle, 1000, ",");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
$res[] = array_combine($keys, $data);

return $res;




Now use as:



$res = setElement([], getCSVarray("array.csv"));


Notice to modify setElement as now the indexes changed!



As:



function setElement($res, $arr) 
if (count($arr) == 0)
return $res;
$e = array_shift($arr);
if (isset($res[$e["parent_id"]]))
$res[$e["parent_id"]]["dates"][$e["id"]] = $e["date"]; // if exist add it
else $res[$e["id]] = array("date" => $e["date"], "dates" => []); // not existed yet - so init it
return setElement($res, $arr);



Edit 2:



Using your array ($MyArray) you can use:



function setElement($res, $arr) 
if (count($arr) == 0)
return $res;
$e = array_shift($arr);
if (isset($res[$e[1]]))
$res[$e[1]]["dates"][$e[0]] = $e[2]; // if exist add it
else $res[$e[0]] = array("date" => $e[2], "dates" => []); // not existed yet - so init it
return setElement($res, $arr);


//using it as:
array_shift($MyArray); // extract first element as it is the keys
$res = setElement([], $MyArray);





share|improve this answer















Every for loop can be converted into recursive function. You can create the following recursion function:



function setElement($res, $arr) 
if (count($arr) == 0)
return $res;
$e = explode(",",array_shift($arr));
if (isset($res[$e[1]]))
$res[$e[1]]["dates"][$e[0]] = $e[2]; // if exist add it
else $res[$e[0]] = array("date" => $e[2], "dates" => []); // not existed yet - so init it
return setElement($res, $arr);



Then use it with (example):



$arr = ["1,0,2017", "2,1,2017-01", "3,1,2017-02", "14,0,2018", "15,14,2018-01", "16,14,2018-02"];
$res = setElement([], $arr);


Now $res will contain your desire output.



Edit:



If your data comes from csv file you need to load them first. You may use fgetcsv as the follow function:



function getCSVarray($path) 
if (($handle = fopen($path, "r")) !== FALSE)
$keys = fgetcsv($handle, 1000, ",");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
$res[] = array_combine($keys, $data);

return $res;




Now use as:



$res = setElement([], getCSVarray("array.csv"));


Notice to modify setElement as now the indexes changed!



As:



function setElement($res, $arr) 
if (count($arr) == 0)
return $res;
$e = array_shift($arr);
if (isset($res[$e["parent_id"]]))
$res[$e["parent_id"]]["dates"][$e["id"]] = $e["date"]; // if exist add it
else $res[$e["id]] = array("date" => $e["date"], "dates" => []); // not existed yet - so init it
return setElement($res, $arr);



Edit 2:



Using your array ($MyArray) you can use:



function setElement($res, $arr) 
if (count($arr) == 0)
return $res;
$e = array_shift($arr);
if (isset($res[$e[1]]))
$res[$e[1]]["dates"][$e[0]] = $e[2]; // if exist add it
else $res[$e[0]] = array("date" => $e[2], "dates" => []); // not existed yet - so init it
return setElement($res, $arr);


//using it as:
array_shift($MyArray); // extract first element as it is the keys
$res = setElement([], $MyArray);






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 28 at 12:36

























answered Mar 27 at 16:54









dWinderdWinder

9,9273 gold badges15 silver badges32 bronze badges




9,9273 gold badges15 silver badges32 bronze badges















  • i am trying to use you're example (which looks great) and i'm using the array like this - $arr = 'array.csv' (the csv file that contains the list, they are in the same folder of course) , and then using this $arr var like you used it, but it is not working for me... any idea why?

    – Àtishking
    Mar 28 at 6:24












  • @Àtishking edit my answer with reading the csv first

    – dWinder
    Mar 28 at 6:34











  • i'm getting this error - Warning: explode() expects parameter 2 to be string, array given in D:ComputershtdocsProjectarray.php this line - $e = explode(",",array_shift($arr));

    – Àtishking
    Mar 28 at 8:49












  • @Àtishking as I said - you need modification - updated my answer accordingly

    – dWinder
    Mar 28 at 9:00











  • first of all - i want to thank you for your patience, this is so not taken for granted. second of all, im using $res = setElement([], $MyArray); (MyArray is the array i loaded from the csv file) and than your function - and it is - still not working. i am getting error of " Undefined index id / parent id / date and so..."

    – Àtishking
    Mar 28 at 9:16


















  • i am trying to use you're example (which looks great) and i'm using the array like this - $arr = 'array.csv' (the csv file that contains the list, they are in the same folder of course) , and then using this $arr var like you used it, but it is not working for me... any idea why?

    – Àtishking
    Mar 28 at 6:24












  • @Àtishking edit my answer with reading the csv first

    – dWinder
    Mar 28 at 6:34











  • i'm getting this error - Warning: explode() expects parameter 2 to be string, array given in D:ComputershtdocsProjectarray.php this line - $e = explode(",",array_shift($arr));

    – Àtishking
    Mar 28 at 8:49












  • @Àtishking as I said - you need modification - updated my answer accordingly

    – dWinder
    Mar 28 at 9:00











  • first of all - i want to thank you for your patience, this is so not taken for granted. second of all, im using $res = setElement([], $MyArray); (MyArray is the array i loaded from the csv file) and than your function - and it is - still not working. i am getting error of " Undefined index id / parent id / date and so..."

    – Àtishking
    Mar 28 at 9:16

















i am trying to use you're example (which looks great) and i'm using the array like this - $arr = 'array.csv' (the csv file that contains the list, they are in the same folder of course) , and then using this $arr var like you used it, but it is not working for me... any idea why?

– Àtishking
Mar 28 at 6:24






i am trying to use you're example (which looks great) and i'm using the array like this - $arr = 'array.csv' (the csv file that contains the list, they are in the same folder of course) , and then using this $arr var like you used it, but it is not working for me... any idea why?

– Àtishking
Mar 28 at 6:24














@Àtishking edit my answer with reading the csv first

– dWinder
Mar 28 at 6:34





@Àtishking edit my answer with reading the csv first

– dWinder
Mar 28 at 6:34













i'm getting this error - Warning: explode() expects parameter 2 to be string, array given in D:ComputershtdocsProjectarray.php this line - $e = explode(",",array_shift($arr));

– Àtishking
Mar 28 at 8:49






i'm getting this error - Warning: explode() expects parameter 2 to be string, array given in D:ComputershtdocsProjectarray.php this line - $e = explode(",",array_shift($arr));

– Àtishking
Mar 28 at 8:49














@Àtishking as I said - you need modification - updated my answer accordingly

– dWinder
Mar 28 at 9:00





@Àtishking as I said - you need modification - updated my answer accordingly

– dWinder
Mar 28 at 9:00













first of all - i want to thank you for your patience, this is so not taken for granted. second of all, im using $res = setElement([], $MyArray); (MyArray is the array i loaded from the csv file) and than your function - and it is - still not working. i am getting error of " Undefined index id / parent id / date and so..."

– Àtishking
Mar 28 at 9:16






first of all - i want to thank you for your patience, this is so not taken for granted. second of all, im using $res = setElement([], $MyArray); (MyArray is the array i loaded from the csv file) and than your function - and it is - still not working. i am getting error of " Undefined index id / parent id / date and so..."

– Àtishking
Mar 28 at 9:16









Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55381725%2fhow-to-use-recursive-function-that-converts-csv-file-to-multidimensional-array%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현