0

Overall Project:

Save multiple arrays from AJAX

Iterating through the array in PHP:

I've managed to successfully save 2 arrays by using array_combine and foreach (see example), but now

Problem:

I have 20+ arrays of dynamic content that I need to iterate into my query statement.

Successful AJAX Sample

var setTitle = new Array();
    $('form input[name="setTitle[]"]').each(function () {
        setTitle.push($(this).val());
    });
    var setUnitId = new Array();
    $('form input[name="setUnitId[]"]').each(function () {
        setUnitId.push($(this).val());
    });

$.ajax({
        type: 'POST',
        url: 'form_process2_wkout.php',
        data: {
            setTitle:setTitle,
            setUnitId:setUnitId,
            // ...and so on...//
        },
        dataType: 'json',
        cache: false,
        async: false

PHP (form_process2_wkout.php) is receiving each $_POST... I've combined them into an array...

    $setArray = array(
            $x_set_title,
            $x_set_unit_id,
            // ...and so on... //
    );

I've var_dump'ed this array to test...

$data= var_dump($setArray);
echo $data;

It produces the following:

array(24) {
    [0]=>
    array(3) {
    [0]=>
    string(9) "Ab Roller"
    [1]=>
    string(16) "Slide Back Lunge"
    [2]=>
    string(10) "Jump Squat"
    }
    [1]=>
    array(3) {
    [0]=>
    string(1) "2"
    [1]=>
    string(3) "879"
    [2]=>
    string(3) "872"
    }
    ...and so on...
}

Not quite sure how write to foreach to iterate through these items:

foreach($setArray as $id => $key) {
    $set_title = mysqli_real_escape_string($dbc, $_POST['setTitle']);
    $set_unit_id = mysqli_real_escape_string($dbc, $_POST['setUnitId']);
    //...and so on.../

}

So that I can use them in the following query:

$sql= "INSERT INTO ".$table1." (`goal_unit_id`,`goal_title`,...and so on...)
       VALUES ";
$sql.="('".$set_unit_id."','".$set_title."',...and so on...)";

I've referenced a number of other approaches, but no luck:

PHP: Iterate over multiple arrays and build SQL INSERT query

array combine three or more arrays with php

Multiple index variables in PHP foreach loop

Community
  • 1
  • 1
cpardon
  • 487
  • 4
  • 24

1 Answers1

0

Use global or class variable to achieve this like below, Please refer arra_map , array_merge api's in PHP

<?php

$x_set_title = array('Ab Roller', 'Slide Back Lunge', 'Jump Squat');
$x_set_unit_id = array('2', '879', '872');

function escape_str($title, $id)
{
   $dbc = $GLOBALS['dbc'];
   $title = mysqli_real_escape_string($dbc, $title);    
   $id = mysqli_real_escape_string($dbc, $id);  
   return (array($title, $id));
}

$setArray = array_map('escape_str', $x_set_title,$x_set_unit_id);

print_r($setArray);

foreach($setArray as $oneArray)
{
    echo $oneArray[0].'  '.$oneArray[1];
}
Sundar
  • 4,580
  • 6
  • 35
  • 61
  • 1
    function was right. There may be an error in passing the parameters http://php.net/manual/en/function.array-map.php check this example and fix the parameter errors – Sundar Jul 30 '15 at 05:10
  • @stackoverflow.com/users/1662383/sundar Read through the php.net manual you provided. Still a bit greek to me. I simplified test of your function... – cpardon Jul 30 '15 at 06:10
  • 1
    check this answer. If you wan to pass multiple parameters then use http://php.net/manual/en/function.func-get-args.php – Sundar Jul 30 '15 at 07:56
  • I've been trying to get my head around where you're 2nd argument ($id) is meant to be generated from? – cpardon Aug 01 '15 at 18:55
  • 1
    when ever the array map function calls the escape_str then $x_set_title array 0th element and $x_set_unit_id 0th element will be passed to the function. Please study further and see the examples in array_map API then you'll understand. Better please execute the Examples in the local machine and get clear idea about that – Sundar Aug 03 '15 at 04:57