0

I am receiving POST data from a form that is an array along with a few other fields. I need to take the array data only and pass it along in a post request of my own to a backend server as json.

The $_POST data looks like this:

Array (
    [smsgte_submit] => Y 
    [alias] => Array (
        [1] => Array ( 
            [name] => mywife
            [number] => 6135552001
            [ssid] => 1 )
        [2] => Array (
            [name] => daughter
            [number] => 6135553001
            [ssid] => ) 
     )
)

I only want to capture the alias entries and encode them in json.

I was successful in encoding the entire $_POST array into json with:

$data['jsonpost'] = json_encode($_POST);

which encoded the array as expected, however, I only want the alias array, so I tried the following:

$data['jsonpost'] = json_encode($_POST['alias']);

That, however doesn't work, it returns null to the server. Then I tried:

$data['jsonpost'] = json_encode(array_filter($_POST, 'alias'));

But that returned null.

Maybe I need to redesign my form, but in the end, I want a json array that looks like this:

    {
    "alias": {
        "name":"mywife",
        "number":"6135552001",
        "ssid":"1"
    },
        "alias": {
        "name":"daughter",
        "number":"6135553001",
        "ssid":"2"
    }
}
6
  • 1
    This should work given what you've shown json_encode($_POST['alias']) however $_POST must not be what you show. print_r($_POST); And what does That, however, doesn't work mean? Commented Jun 5, 2019 at 18:15
  • I updated the post to reflect the output of print_r($_POST); however, other than laying it out more clearly as an array, I don't believe anything has changed. json_encode($_POST['alias']) sends null to the server. Commented Jun 5, 2019 at 18:39
  • 1
    I don't think I trust you. json_encode returns a string or false. Checking for errors and warnings? Commented Jun 5, 2019 at 18:54
  • Can you show us the results of print_r($_POST); Commented Jun 5, 2019 at 19:33
  • The output of print_r($_POST) is Array ( [smsgte_submit] => Y [alias] => Array ( [1] => Array ( [name] => mywife [number] => 6135552001 [ssid] => 1 ) [2] => Array ( [name] => daughter [number] => 6135553001 [ssid] => 2) ) ) Commented Jun 5, 2019 at 19:47

1 Answer 1

0

It turns out that the following syntax should be correct:

$data['jsonpost'] = json_encode($_POST['alias']);

However, in order to get it to work, I had to split it as follows:

$jsonpost = json_encode($_POST['alias']);
$data['jsonpost'] = jsonpost;

I switched back and forth between the two options a few times, but only the second one works, at least with PHP 7.3.5.

Sign up to request clarification or add additional context in comments.

2 Comments

Not sure this answer reality answers anything. You are probably overlooking a warning about Cannot use a scalar value as an array or Illegal string offset which would shed light on what you were doing and why it was wrong.
I wasn't presented any warnings.

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.