Jump to content

foreach loop for multidimensional array


dmacman

Recommended Posts

Hi everyone,

 

I am can do this with a one dim. array, but when I get back what I need, like this..

 


[Class] => Array
        (
            [0] => Array
                (
                    [1] => 1
                    [2] => 2
                )

            [1] => Array
                (
                    [3] => 3
                    [4] => 4
                )

            [2] => Array
                (
                    [5] => 5
                    [6] => 6
                )

        )

 

I get messed up.

 

With my other arrays, I would do this...

 

foreach($_POST['Class'] $BallotID => as $ClassID)
	{
			//insert ratings data into MySQL
				$sql ="insert into classes"; 
				$sql .="(BallotID, ClassID, Date)";
				$sql .="values('$BallotID', '$ClassID', '$Date')";
					if(!mysql_query($sql))
						{
						echo 'insert into classes error<br><br>' . mysql_errno().'<br>'.mysql_error().'<br><br>';
						}				
	}

 

But that of course, gets me only the last item chosen.

 

I appreciate the help,

 

Don

Link to comment
https://forums.phpfreaks.com/topic/101414-foreach-loop-for-multidimensional-array/
Share on other sites

I want to insert the data.

 

What I need to get is: (and if I am totally wacko on this, please let me know, since I am not a multi dim guru).

 

The BallotID was 1 (from the query before this, and I just need to use that for this insert, and not change it)

 

And I need to insert the for the chosen checkboxes array index, and which ones they picked.

 

so...

 

BallotID  ClassID  ClassesID

1----------0--------1

1----------0--------2

1----------1--------3

1----------1--------4

1----------2--------5

1----------2--------6

 

And this is from the POST data of

 

[Class] => Array

        (

            [0] => Array

                (

                    [1] => 1

                    [2] => 2

                )

 

            [1] => Array

                (

                    [3] => 3

                    [4] => 4

                )

 

            [2] => Array

                (

                    [5] => 5

                    [6] => 6

                )

 

        )

 

Does that make sense?

 

Thanks,

 

Don

 

 

 

do you mean

<?php
$class = Array
        (
            Array
                (
                    1 => 1,
                    2 => 2
                ),

            Array
                (
                    3 => 3,
                    4 => 4
                ),

            Array
                (
                    5 => 5 ,
                    6 => 6
                )

        );

$ballotid = 1;
foreach ($class as $classID => $classesArray)
{
    foreach ($classesArray as $classesID)
    {
        $sql = "INSERT INTO classes (ballot, classID, classesID) VALUES ($ballotid, $classID, $classesID)";
        echo $sql, '<br>';
#        mysql_query($sql);       // commented to test
    }
}
?>

Hi Barand,

 

That worked great, but, for my use, I had to change it to:

 

foreach ($_POST['Class'] as $ProductID => $classesArray)
			{
				foreach ($classesArray as $ClassID)
						{
						   //insert ratings data into MySQL
							$sql ="insert into classes"; 
							$sql .="(BallotID, ProductID, ClassID, Date)";
							$sql .="values('$BallotID', '$ProductID', '$ClassID', '$Date')";
								if(!mysql_query($sql))
									{
									echo 'insert into classes error<br><br>' . mysql_errno().'<br>'.mysql_error().'<br><br>';
									}
						{
			{

 

But the question I have is, the array of course, start with zero, as they all do.

 

I need the $ProductID to start with 1, how would I do that in this instance?

 

Thanks,

Don

I get what your saying, but that is if I am defining the array.

 

This array is from $_POST['Class'] data from the previous page, which is set to zero by default.

 

So how can I have it start at 1 or at least, add 1 to all the indexes in the loop.

 

Or is that too tough to do?

 

Thanks,

Don

try

<?php
if (isset($_POST['btn']))
{
    $classes = array_merge(array('dummy'), array_values($_POST['class']));        // put dummy value in pos 0
    unset($classes[0]);                                                           // remove dummy
    foreach ($classes as $k => $v)
    {
        echo "$k $v <br>";                                                        // remaining array starts at 1
    }
}

?>

Thanks barand,

 

I will put your fix in my saved code library.

 

I made a stupid mistake.

 

I forgot to go back and get the ProductID from the first form from the database and I had hard-coded it with the ID's starting at 0

 

<input name="Products[0]"
//etc

 

And I went back here and changed it to get the data from the table, and now I am in sync (starting with 1 as I should be.

 

But you fix is something I can still use in another thing I was thinking of, thanks.

 

I owe you one.

 

regards,

Don

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.