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.