Build SQL Query from multiple arrays / PHP

I've got an array that looks like this..

Array
(
    [fm] => Array
        (
            [133.74] => Array
                (
                    [base] => Array
                        (
                            [0] => 2015-09-29
                            [1] => 2015-09-30
                        )

                )

            [202.59] => Array
                (
                    [base] => Array
                        (
                            [0] => 2015-10-01
                        )

                )

        )

    [fmtax] => Array
        (
            [13.51] => Array
                (
                    [0] => 2015-09-29
                    [1] => 2015-09-30
                )

            [20.46] => Array
                (
                    [0] => 2015-10-01
                )

        )

)

Open in new window


and I need to build a SQL statement to insert the relevant values.  I'm thinking it will need to be a unique query for each array  item.

So I'll end up needing something like

$sql = "insert into mytable ('price', 'tax', 'date') values ('133.74', '13.51', '2015-09-29')";

What I've got so far is this...

foreach ($sale['fm'] as $key => $value) {
	$i=0;
	$g=count($value['base']);
	while($i < $g) {
		echo($sale['fm'][$key]['base'][$i]."<br />");
		$i++;
	}
}

Open in new window


which does well for the "fm" key - but I'm getting hung up on how to grab the value from the second associated "fmtax" array key.  I was thinking using something like current/next as it iterates through...but am kinda scratching my head on it.

Any help would be appreciated :)
LVL 2
erzoolanderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
$data=Array
(
    'fm' => Array
        (
            '133.74' => Array
                (
                    'base' => Array
                        (
                            '0' => '2015-09-29'
                            ,'1' => '2015-09-30'
                        )

                )

            ,'202.59' => Array
                (
                    'base' => Array
                        (
                            '0' => '2015-10-01'
                        )

                )

        )

    ,'fmtax' => Array
        (
            '13.51' => Array
                (
                    '0' => '2015-09-29'
                    ,'1' => '2015-09-30'
                )

            ,'20.46' => Array
                (
                    '0' => '2015-10-01'
                )

        )

);

$tax = array_keys($data['fmtax']);

$i=0;
foreach($data['fm'] as $price=>$temp)
{
	$sql = "insert into mytable ('price', 'tax', 'date') values";
	$vals=array();
	foreach($temp['base'] as $date)
	{
		$vals[]=sprintf(" (%.2f, %.2f, '%s')",$price,$tax[$i],$date);
	}
	$sql .= implode(',',$vals) . ';';
	echo $sql.'<br/>';
	++$i;
}

Open in new window

0
Ray PaseurCommented:
The array does not make sense from an information architecture perspective.  I don't know where it might have come from, but an active-record design pattern would strongly suggest that one array or object should exactly match one row of the database.  Each element of the main array would represent a single row of the database and would be processed by a single INSERT statement.  Something like this:
/**
 * DESIRED QUERY: $sql = "insert into mytable ('price', 'tax', 'date') values ('133.74', '13.51', '2015-09-29')";
 *
 * DO NOT USE THIS DATA STRUCTURE
Array
(
    [fm] => Array
        (
            [133.74] => Array
                (
                    [base] => Array
                        (
                            [0] => 2015-09-29
                            [1] => 2015-09-30
                        )

                )

            [202.59] => Array
                (
                    [base] => Array
                        (
                            [0] => 2015-10-01
                        )

                )

        )

    [fmtax] => Array
        (
            [13.51] => Array
                (
                    [0] => 2015-09-29
                    [1] => 2015-09-30
                )

            [20.46] => Array
                (
                    [0] => 2015-10-01
                )

        )

)
 */

/**
 * INSTEAD CHOOSE SOMETHING LIKE THIS
 */
$arr = array
( [ 'price' => 133.74, 'tax' => 13.51, 'date' => '2015-09-29' ]
, [ 'price' => 133.74, 'tax' => 13.51, 'date' => '2015-09-30' ]
, [ 'price' => 202.59, 'tax' => 20.46, 'date' => '2015-10-01' ]
)
;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.