Web Analytics Made Easy -
StatCounter Can I populate a sequenced array with data from an associative array using a loop? - CodingForum

Announcement

Collapse
No announcement yet.

Can I populate a sequenced array with data from an associative array using a loop?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can I populate a sequenced array with data from an associative array using a loop?

    I have an html form that passes infomation from fifteen sets of eight text inputs using the POST method. I separated the data in the associative array, $_POST, into eight sequenced arrays manually. The code below shows one of these arrays being populated:
    PHP Code:
    $arr_dsh[0] = $_POST["dsh_01"];
    $arr_dsh[1] = $_POST["dsh_02"];
    $arr_dsh[2] = $_POST["dsh_03"];
    $arr_dsh[3] = $_POST["dsh_04"];
    $arr_dsh[4] = $_POST["dsh_05"];
    $arr_dsh[5] = $_POST["dsh_06"];
    $arr_dsh[6] = $_POST["dsh_07"];
    $arr_dsh[7] = $_POST["dsh_08"];
    $arr_dsh[8] = $_POST["dsh_09"];
    $arr_dsh[9] = $_POST["dsh_10"];
    $arr_dsh[10] = $_POST["dsh_11"];
    $arr_dsh[11] = $_POST["dsh_12"];
    $arr_dsh[12] = $_POST["dsh_13"];
    $arr_dsh[13] = $_POST["dsh_14"];
    $arr_dsh[14] = $_POST["dsh_15"]; 
    The data from these arrays are then passed to a function, one element at a time using a while loop:
    PHP Code:
    $c 0;
    while (
    $c <= 14) {
        
    $gross_prod $gross_prod calc_gross($arr_dsh[$c], $arr_deh[$c], $arr_dem[$c], $arr_dsm[$c], $arr_leh[$c], $arr_lem[$c], $arr_lsh[$c], $arr_lsm[$c]);
        
    $c++;
        } 
    Is it possible to populate the array, $arr_dsh, using a loop? As it stands, I have more than 100 lines of code just fleshing out the arrays.

    tia.
    Last edited by golgotha; Sep 25, 2006, 10:32 AM.

  • #2
    Try this one.
    PHP Code:
    for ($i=0$j=1$i<count($_POST); $i++, j++)  {
       
    $n=sprintf("%02s"$j);
       
    $arr_dsh[$i] = $_POST[dsh_$n];

    Ronald
    RTFM is an almost extinct art form, it should be subsidized.

    Comment


    • #3
      In Ronverdonk's example you'll want to put the string part of the index name in quotes and concantenate the string with the variable.

      $_POST['dsh_'.$n];

      But you may be better off in the long run using an array on your form elements instead of naming them dsh_1, dsh_2, etc.

      PHP Code:
      <input type="text" name="dsh[]">
      <input type="text" name="dsh[]">
      <input type="text" name="dsh[]">

      <?php
      //When form is submitted, $_POST will contain an array of the dsh[] values
      echo $_POST[dsh][0];
      echo 
      $_POST[dsh][1];
      echo 
      $_POST[dsh][2];
      ?>

      Comment


      • #4
        PHP allows you to "push" a new item onto an array without knowing the index:

        Code:
        $indexedArray = array();
        foreach ($associativeArray AS $key=>$value)
        {
          $indexedArray[] = $value;
        }

        Comment


        • #5
          if you're going to assume the associative array is ordered properly, you can just use the array_values function
          http://php.net/array-values
          My thoughts on some things: http://codemeetsmusic.com
          And my scrapbook of cool things: http://gjones.tumblr.com

          Comment


          • #6
            some good ideas here. it will take me some time to experiment with each of them, and find the best solution.
            I coded the html and wrote the main function. I was staring at all the repetitive information and I said to myself: I bet a loop would work here.

            This is all very new to me, but i'm having fun putting all the pieces together. Once I get form and array handling under control, I think the next step is a database to collect the input and store it for later use.

            this is a big help. thanks

            Comment

            Working...
            X