Jump to content

array combinations


searls03

Recommended Posts

Ok, So what I need is a way for arrays to combine.  My basic structure is going to look like this for the form that needs submission:

 

<form>
<input name="name[]" />
<input name="name1[]" />
<input name="name[]" />
<input name="name1[]" /> 
<input name="name[]" />
<input name="name1[]" /> 
</form>

then on the page it submits to, I need an array to form that will combine all the inputs to looks something like this:

 

"name" "name1"; "name" "name1"; "name" "name1";..........

 

where the first set is the first name and name1, second is second, third is third, etc. 

The string of arrays will only end when there are no more left, so if there were 6, it would combine 6.  how do I do something like this?

Link to comment
https://forums.phpfreaks.com/topic/260029-array-combinations/
Share on other sites

Then just change your field names so the data will already be in the format you need.

<form>
<input name="names[0]['name']" />
<input name="names[0]['name1']" />
<input name="names[1]['name']" />
<input name="names[1]['name1']" />
<input name="names[2]['name']" />
<input name="names[2]['name1']" /> 
</form>

 

Although it would be best to create the fields using PHP code

for($i=0; $i<3; $i++)
{
    echo "<input name=\"names[0]['name']\" />\n";
    echo "<input name=\"names[0]['name1']\" />\n";
}

 

The submitted data will be in this format

Array
(
    [0] => Array
        (
            [name]  => name from first set
            [name1] => name1 from first set
        )
    [1] => Array
        (
            [name]  => name from second set
            [name1] => name1 from second set
        )
    [2] => Array
        (
            [name]  => name from third set
            [name1] => name1 from third set
        )
)

Link to comment
https://forums.phpfreaks.com/topic/260029-array-combinations/#findComment-1332830
Share on other sites

psycho, did you mean:

 

for($i=0; $i<3; $i++)
{
    echo "<input name=\"names[$i]['name']\" />\n";
    echo "<input name=\"names[$i]['name1']\" />\n";
}

 

Yes, I did. BUt, on second review I also forgot that you don't put quotes around the index names in the HTML form, so it should actually be like this:

for($i=0; $i<3; $i++)
{
    echo "<input name=\"names[$i][name]\" />\n";
    echo "<input name=\"names[$i][name1]\" />\n";
}

 

 

 

how exactly do they combine then?  I am bad at writing arrays.  Never have been able to, maybe someday will.  anyways, could you help me with that.

 

Seriously? Please re-read what I said. I told you that by creating the input fields with names in that format the data would ALREADY be in the format you requested and I even provided an example of what that array would look like.

Link to comment
https://forums.phpfreaks.com/topic/260029-array-combinations/#findComment-1332890
Share on other sites

the code is like this :

<?php
// Query member data from the database and ready it for display
$sql4 = mysql_query("SELECT * FROM labels where item_id='".$pid."'");
while($row = mysql_fetch_array($sql4))for($i=0; $i<1; $i++){

$label =$row["label"];
$lid=$row['id'];
echo $label; 
?>
<?php

    echo "<input name=\"names[$i][name1]\" value=\"$label\" />\n";
        echo "<select name=\"names[$i][name]\" />\n";


?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/260029-array-combinations/#findComment-1333066
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.