0

I give up. I'm sending form data to PHP via ajax using json.serializeArray().

In it's raw form it arrives like this:

["0":{
    "name": "first_name",
    "value": "BILLY"
    }, 
"1":{
    "name": "phone",
    "value": "04532423"
    }
]

In PHP, I'm trying to store/access the values. So I loop through using something like:

$new_data = array();
for($i = 0; $i < count($data); $i++) {
  $obj = new stdClass();
  $key = $data[$i]['name'];
  $val = $data[$i]['value'];
  $obj->$key = $val;
  array_push($new_data, $obj);
}

Which results in:

[
    {
        "first_name": "test"
    },
    {
        "phone": "0422335656"
    }
]

I then try to simplify the array further using a function:

$form_data = $_POST['order_details'];
function simple_merge($data) {
    $new_data = array();
    $new_obj = new stdClass();
    for ($i = 0; $i < count($data); $i++) {
        $obj = new stdClass();
        $key = $data[$i]['name'];
        $val = $data[$i]['value'];
        $obj - > $key = $val;
        array_push($new_data, $obj);
    };
    foreach($new_data as $key => $value) {
        $new_obj - > $key = $value;
    }
    return $new_obj;
}

$response = simple_merge($form_data);

echo json_encode($response);

Which should result in a single object I can access using something like $data->[$key] but the results are returning as null.

I've tried simplifying the form data to JSON before sending it, but am having similar results.

For more context, I'm trying to create a new order in WooCommerce, and to debug I'm storing the results in an object and then returning them using echo json_decode($response)

I have a feeling that this is one of those problems where the solution is going to have me facepalming.

Joseph P
  • 35
  • 5
  • Hi first error you used a variable named new_data but never defined in your code – Inazo Jul 28 '22 at 08:24
  • `["0": {...}]` is not valid JSON… – deceze Jul 28 '22 at 08:25
  • If it's `{"0": {"first_name": ...}}`, then you access it like `$data["0"]["first_name"]`, not `$data->first_name` or `$data["first_name"]`… – deceze Jul 28 '22 at 08:27
  • sorry, I forgot to mention that. It sits outside the for loop. Will edit that now. – Joseph P Jul 28 '22 at 08:27
  • @deceze yes, I have accessed them like that within my for loop, and then turned them into the second array. – Joseph P Jul 28 '22 at 08:30
  • Is `[{"first_name": ...}, {...}]` the desired outcome?! Or do you want `{"first_name": ..., "phone": ...}`? – deceze Jul 28 '22 at 08:39
  • @deceze I believe the latter is the one I want, so I can access the values by using something like `$data->first_name` or `$data['first_name']`. I have also updated the post with another method I've tried – Joseph P Jul 28 '22 at 08:42
  • Then instead of *pushing an object* into `$new_data`, you just want `$new_data[$key] = $value`…?! – deceze Jul 28 '22 at 08:43
  • @deceze That's correct. And I've tried that by wrapping it in a function. I couldn't get the value to display in a response for some reason. – Joseph P Jul 28 '22 at 08:46
  • …? What's that mean exactly? Show your attempt? – deceze Jul 28 '22 at 08:47
  • @deceze As I mentioned in an earlier comment, I've updated the post with the additional code... – Joseph P Jul 28 '22 at 08:49
  • 1
    `function simple_merge($data) { $new = []; foreach ($data as $d) { $new[$d['name']] = $d['value']; } return $new; }`…?! – deceze Jul 28 '22 at 08:52
  • @deceze, that worked! Thank you! I had to add an $i before ['name']. If you would like to post your solution I can mark yours as best answer. – Joseph P Jul 28 '22 at 09:13

0 Answers0