I’m attempting to load a comma-separated string of values from a database cell (e.g. 1,2,3,4,5,6,7,8,9,10,11,12) into a two-dimensional array.
There is another database cell containing a comma-separated string of table column headings, which dictate the number of sub-arrays within the main array.

So in the case of the example data above, I’d like the array to be something like this:

$data_table_array = array (
array(1,5,9),
array(2,6,10),
array(3,7,11),
array (4,8,12),
);

but to be able to automate this with a loop of some sort.

The data points populate a table with as many columns as column headings. I’ve got this working nicely but I decided that I wanted to give the user and option to add or delete columns as well as rows and editing the actual data. I believe that using this 2D array allows the flexibility to do this. – Or am I mistaken?

Sadly, I don’t know how to loop the data into the multidimensional array.
I have initially exploded the data into an array and I’m attempting to create a nested loop that will take me to the next stage. I wondered whether anyone out there could give me an example of a code structure that can achieve this?

I can provide further details of my problem but I didn’t want to confuse and complicate the issue too much.

I would very much appreciate it if someone can share their expertise with me for this very frustrating problem.

Many thanks in advance for your assistance.

Jason Green. N. London, UK

    to get the data from the inital comma separated list use explode
    then as far as the other half I am not sure but that should get you started

      $x=$y=0;
      $ar=explode(",", "1,2,3,4,5,6,7,8,9,10,11,12");
      
      while ($x<4) {
            $array1[$x]=$ar[$y];
            $y++;
            $array2[$x]=$ar[$y];
            $y++;
            $array3[$x]=$ar[$y];
            $y++;
            $array4[$x]=$ar[$y];
            $y++;
            $x++;
       }
      $bigarray=array($array1, $array2, $array3, $array4);

      Gee, that's one awful hack ... I must be the master of the kludge.... :rolleyes:

        Many thanks for your suggestion. That has certainly helped me with understanding this a bit better.

        It's definitely a step in the right direction but doesn't do the whole job yet.
        I mentioned previously that I have a number of array of column headings which dictate the number of $arrays within $bigarray.
        As you can see below, I've counted the elements within $heading_ar and used it to set the number of executions of the while loop. This is because the number of heading elements is likely to change.
        How do I adapt this to replace $array1, $array2 etc with an automatic increment of $array_"$x" ?

        Presumably I create another loop within the original loop?

        $heading_ar=explode(",", "heading_1,heading_2,heading_3,heading_4");
        $count= count($heading_ar);
        $x=$y=0;
        $ar=explode(",", "1,2,3,4,5,6,7,8,9,10,11,12");
        
        while ($x<($count)) {
              $array1[$x]=$ar[$y];
              $y++;
              $array2[$x]=$ar[$y];
              $y++;
              $array3[$x]=$ar[$y];
              $y++;
              $array4[$x]=$ar[$y];
              $y++;
              $x++;
         }
        $bigarray=array($array1, $array2, $array3, $array4);
        

        Have you or any one else by any chance got another neat 'hack' for this?!

        Much appreciating your help so far.

        With kind regards,

        Jason.

        p.s. What's a kudge

        p.ps. Not trying to be smart or anything but for the benefit of others who might use this thread, an Undefined Offset error occured when I executed the script. I found this to be because "($x<4)" should be "($x<3)".

          Originally posted by greeno76
          p.s. What's a kudge

          😃 It was "kludge." More properly, that should be "kluge".

          Fascinating reading if you've the time; all "hackers" should probably read Eric Raymond's "Jargon File". Note the differences between these two terms. I probably meant "kluge" but when I started coding and reading about coding I always saw the other spelling....

          http://www.catb.org/~esr/jargon/html/K/kludge.html
          http://www.catb.org/~esr/jargon/html/K/kluge.html

          [edit]I'd love to continue helping, but at the present I'm demanded elsewhere. I'll try and remember to check back later today or tonight.

          I have a feeling we're going to have to go to the manual for an array function or two to get this exactly how you want it. With luck, maybe one of the gurus will come by and give us an elegant one liner .... 😉[/edit]

            "Something that works for the wrong reason " - In which case I've definitely kluged this!

            Please excuse my untidy coding but I've got it working at least.

            The code below adapts the number of arrays and array elements depending on the initial comma-delimited values.

            //set, explode & count heading data
            $heading_ar=explode(",", "heading_1,heading_2,heading_3,heading_4,heading_5");
            $count= count($heading_ar);
            //set, explode & count data points
            $ar=explode(",", "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20");
            $count_ar= count($ar);
            
            $data_loop =$count_ar/$count;
            
            
            //put each data point from $ar into $array_#
            $y=0;
            
            
            for ($x=0;$x<$data_loop;$x++) {
            
            $array_number=0;
            
            foreach($heading_ar as $heading_data){
            
            
            	  ${"array" . $array_number}[]=$ar[$y];
            
            $y++;
            $array_number++;
               }
            
            }
            
            //put each $array_# into $bigarray
            $bigarray_loop=0;
            
            while ($bigarray_loop<$count)
            	{
             	$bigarray[]=${"array" . $bigarray_loop};
            	$bigarray_loop++;
            	}
            
            //print out the arrays
            $j=0;
            foreach($heading_ar as $heading_data){
            	for ($i=0;$i<$data_loop;$i++) {
            
            
              echo ($bigarray[$j][$i]);
              ?><BR><?
            
               }
            	$j++;
            	?><BR><?
            }
            
            

            If you have a neater solution then I'd love to hear it, of course!

            The next challenge is to get all this into a tabulated form which rows and columns can be edited. I'm thinking that array_splice() is going to come in handy?

            In the meantime, many thanks for your help and your insight into hacker's jargon!

            Best wishes,

            Jason

              Actually, I find your solution perfectly acceptable ... as I implied, I'm no guru just 'cause I spend all my time posting on this forum! The curly brackets are a neat thought, indeed...it does seem like I've used $var$y as a variable name before, dunno that I've needed it for arrays.

              As for the intro to Hackish, you're quite welcome! Have fun with PHP!! 🙂

                Write a Reply...