I upload a csv and expload it to an array.
array(6) {
[0]=>
string(64) "Criteron A-1 bla ,Criteron A-2 bla,Criteron A-3 bla,Criteron A-4 bla,Criteron A-5 bla"
[1]=>
string(64) "Criteron B-1 bla,Criteron B-2 bla,Criteron B-3 bla,Criteron B-4 bla,Criteron B-5 bla"
[2]=>
string(51) "Criteron C-1 bla,Criteron C-2 bla,Criteron C-3 bla,Criteron C-4 bla"
[3]=>
string(38) "Criteron D-1 bla,Criteron D-2 bla,Criteron D-3 bla"
[4]=>
string(64) "Criteron E-1,Criteron E-2,Criteron E-3,Criteron E-4,Criteron E-5"
[5]=>
string(51) "Criteron F-1,Criteron F-2,Criteron F-3,Criteron F-4"
}
The original csv has 6 rows and max 5 columns as you see in the above.
In order to put these data in a table, I need to know number of rows and maximum number of columns.
I can find number of rows by using count(). I am thinking to use substr_count() of comma “,” for each array and find max then add one to that max to find out the max number of column. (I haven’t figure it out how to code the second part yet, though)
I am wondering if you know a better way.
Thanks in advance.
You can get the max number of columns using
max(),array_map()andexplode()array_mapruns the anonymous function for every row in the array, creating a new array with its results.For each row,
count(explode())counts the number of columns seperated by commas in it.max()runs on the array of column counts, returning the largest one.