0

I am posting an array from javascript using AJAX

$.ajax({
        url: "test.php",
        type: "POST",
        data: {arr:fullData},

        success: function (data) {
            console.log(data);
            document.getElementById("status").innerHTML = data;

        },

        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("Status: " + textStatus);
            alert("Error: " + errorThrown);
        }

This is the array:

0: "event=200 Yard Free Style&swimmername=joe fish&lane=1&time=00:00:00&place=0&scoreA=0&scoreB=0&place2=0&time2=00:00:00&lane2=2&swimmername2=joe fish"
1: "event=200 Yard Free Style&swimmername=steve jobs&lane=3&time=00:00:00&place=0&scoreA=0&scoreB=0&place2=0&time2=00:00:00&lane2=4&swimmername2=steve jobs"
2: "event=200 Yard Free Style&swimmername=steve jobs&lane=5&time=00:00:00&place=0&scoreA=0&scoreB=0&place2=0&time2=00:00:00&lane2=6&swimmername2=steve jobs"
length: 3

In PHP, The array comes through like this:

array (size=1)
  'arr' => 
    array (size=3)
      0 => string 'event=200 Yard Free Style&swimmername=joe fish&lane=1&time=00:00:00&place=0&scoreA=0&scoreB=0&place2=0&time2=00:00:00&lane2=2&swimmername2=joe fish' (length=147)
      1 => string 'event=200 Yard Free Style&swimmername=steve jobs&lane=3&time=00:00:00&place=0&scoreA=0&scoreB=0&place2=0&time2=00:00:00&lane2=4&swimmername2=steve jobs' (length=151)
      2 => string 'event=200 Yard Free Style&swimmername=steve jobs&lane=5&time=00:00:00&place=0&scoreA=0&scoreB=0&place2=0&time2=00:00:00&lane2=6&swimmername2=steve jobs' (length=151)

I have tried using "explode()" but it keeps saying it can't explode an "array"

I am trying to do the following:

 $array = $_POST;
 var_dump ($array[0]['event'] );

and set the different values to build a sql query like so (in loop):

 foreach($_POST as $data)
{
    $sql_query = '(';

if(isset($array['event'])){$sql_query .= $array['event'] . ',';}
if(isset($array['event'])){$sql_query .= $array['swimmername'] . ',';}

I am new to PHP and having a tough time figuring it out. Any help will be appreciated.

PHP Code:

<?php

$array = $_POST;

$array1 = $_REQUEST['arr'];
var_dump($array['arr']);

$returnedInfo = explode('&',$array[1]);
// Empty string when using an empty array:
echo ($returnedInfo);
$sql_query = 'INSERT INTO tbl_name
    (event,swimmername, lane, place, time, score,  )
    VALUES ';
$i = 0;
$arrayLength = count($_POST);
var_dump ($arrayLength);

foreach($_POST as $data)
{


    $sql_query = '(';
    if(isset($data['event'])){$sql_query .= $data['event'] . ',';}
    if(isset($data['swimmername'])){$sql_query .=  ','. $data['swimmername'] . ',';}
    if(isset($data['lane'])){$sql_query .=  ','.  $data['lane'] . ',';}
    if(isset($data['place'])){$sql_query .=  ','. $data['place'] . ',';}
    if(isset($data['time'])){$sql_query .=  ','. $data['time'] . ',';}
    if(isset($data['scoreA'])){$sql_query .=  ','.  $data['scoreA'] . ',';}
    if(isset($data['scoreB'])){$sql_query .=  ','. $data['scoreB'] . ',';}
    if(isset($data['time2'])){$sql_query .=  ','. $data['time2'] . ',';}
    if(isset($data['lane2'])){$sql_query .=  ','.  $data['lane2'] . ',';}
    if(isset($data['place2'])){$sql_query .=  ','. $data['place2'] . ',';}
    if(isset($data['swimmername2'])){$name2[] = $data['swimmername2'] . ',';}
    $i++;
    if($i < $arrayLength) {
        $sql_query .=  '),';
    } else {
        $sql_query .=  ');';
    }


}
// Then use $sql_query for the INSERT step.
?>
13
  • Look this stackoverflow.com/questions/6507679/… Commented Nov 13, 2015 at 16:42
  • are you trying to explode('&',$array) or explode('&',$array[0]) ?? It seems like the second version should work but the first would give the error you mentioned Commented Nov 13, 2015 at 16:42
  • I tried that, but my issue is when I use explode, I get: explode() expects parameter 2 to be string, array given even though my output shows: 0 => string 'event=200 Yard Free Commented Nov 13, 2015 at 16:44
  • Let me try that. :-) Commented Nov 13, 2015 at 16:45
  • The key of the array is arr. The first level/dimension I mean. Commented Nov 13, 2015 at 16:45

1 Answer 1

1

Assuming your array looks like this:

array (
  'arr' => 
  array (
    0 => 'event=200 Yard Free Style&swimmername=joe fish&lane=1&time=00:00:00&place=0&scoreA=0&scoreB=0&place2=0&time2=00:00:00&lane2=2&swimmername2=joe fish',
    1 => 'event=200 Yard Free Style&swimmername=steve jobs&lane=3&time=00:00:00&place=0&scoreA=0&scoreB=0&place2=0&time2=00:00:00&lane2=4&swimmername2=steve jobs',
    2 => 'event=200 Yard Free Style&swimmername=steve jobs&lane=5&time=00:00:00&place=0&scoreA=0&scoreB=0&place2=0&time2=00:00:00&lane2=6&swimmername2=steve jobs',
  ),
)

...you simply need to loop through it and use something like parse_str (easy) or explode (slightly more work) to transform each string into an array. For example:

# Grab the 'arr' array
$realArray = isset($array['arr']) ? $array['arr'] : array();
foreach ( $realArray as $k => $v ) {
    # Quick and painless way to convert to array
    parse_str($v, $value);
    $realArray[$k] = $value;
}
# Put this into a function called 'debug' to make life easier
echo '<pre>';
var_export($realArray);
echo '</pre>';
Sign up to request clarification or add additional context in comments.

2 Comments

That looks good. How can I get "event" = "200..." back as a separate object with out explode?
I get it: var_export($value['event']);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.