Time to learn arrays... hmm


#1

Well a quick question here. Basically I am trying to learn the concept of arrays, and this is what I am trying to do.
On the form page we have all our variables and are ready to be filled out. $_POST is the array created upon submiting the form right? No problem.

Now we make things a little more difficult. With each field name I want to use [require] and [norequire] IE

Thus on the next page it would show up as
an array $_POST[require][name] As well as bringing in the rest of the fields, under each array, require and norequire. ( I hate multidimensional arrays)

My question is, when bringing out the data in the array, how do I keep the New Array Order the same as the order it was submitted from the form?

IE.

This order will show up in the array like this, with two seperate arrays:
[require] [name] => bob
[require] [phone] => 555-5555
[norequire] [email] => bob@555.com
[require] [phone] => 555-5555
[norequire] [content] => blah

Ideas or suggestions? Or is this not possible? Basically What im trying to do is create a universal function that will take any submited information and insert it into the database, this would not be a problem if i didn’t want to do error checking, but the error checking and throwing me into multi-dimensional arrays kinda throws me way off… any ideas or suggestions would be greatly appreciated!! Thanks,
Drew

A Fun little challenege indeed, but I need to understand how arrays work first.


#2

Let me make sure I am understanding this correctly - You are working with a 3 dimensional array ($_POST, required or not, data"type")!!! COOL. So you want to order them… I guess you can force it by writing a function to take the individual parts and putting them into a new array in the order you specify. This is off the top of my head so I am sure there are a million things wrong with it but…

Psuedocode:

function buildMyArray($array1, $array2)
{
    // find out which array is bigger.... guess you could do a ternary here
    if (count($array1) > count($array2))
    {
        $max = count($array1);
    }
    else
    {
        $max = count($array2)
    }

    // load myArray - make sure you handle if one of the arrays is 
    //bigger then the other... maybe set the value to null or use array_pad
    for (i = 0; i <$max; i++)
    {
         $myArray[i] .= "$array1[i] => $array1[value]";
         $myArray[i] .= "$array2[i] => $array2[value]";   
    }
    return $myArray;    
}

let me know if I am way off base… happens more often then not.