-2

I am trying to encode an associative array in php

$data = array(
        "firstName" => $_POST["firstName"],
        "lastName" => $_POST["lastName"],
        "email" => $_POST["email"],
        "telephone" => $_POST["telephone"]
);

however the output is in string format and not in array format.

string(98) "{"firstName":"rob","lastName":"shelford","email":"some@domain.co.uk","telephone":"01245454545"}"

The output needs to have the square brackets so the receiving server can read the data correctly.

Is there another syntax for the $data array I need to use?

EDIT 1

The offical PHP documentation https://www.php.net/manual/en/function.json-encode.php

states that

echo "Normal: ",  json_encode($a), "\n";

can be used to output

Normal: ["<foo>","'bar'","\"baz\"","&blong&","\u00e9"]

however when I try to use the "\n" flag, I receive an error

EDIT 2

The entire data I wish to convert

$data = array(
        "applicants" => array(
          "firstName" => $_POST["firstName"],
          "lastName" => $_POST["lastName"],
          "email" => $_POST["email"],
          "telephone" => $_POST["telephone"]
        ),
    "buyerType" => $_POST["buyerType"],
    "organisationId" => "xxxxxxxxxx",
    "introducerId" => "0",
    "introducerBranchId" => "0",
    "introducerNegotiatorId" => "0",
);

EDIT 3 I have made it work by passing this into json_encode() :

$data = array(

  "applicants"=> [
    array(
      "email"=> $_POST["email"],
      "firstName"=> $_POST["firstName"],
      "lastName"=> $_POST["lastName"],
      "telephone"=> $_POST["telephone"]
    )
  ],
  "buyerType" => $_POST["buyerType"],
  "organisationId" => "xxxxxxxxxx",
);

I have also removed the unnecessary lines in the above example

EDIT 4

The actual output I was expecting, and actually got, after implementing EDIT3

string(138) "{"applicants":[{"email":"some@domain.co.uk","firstName":"rob","lastName":"test","telephone":"01200000000"}],"buyerType":"Purchase"}"
Yenmangu
  • 71
  • 9
  • Can you show the expected JSON string? – nice_dev Jun 24 '22 at 12:18
  • You encode your answer $json = json_encode($data); and the receiver decodes it $receiver = json_decode($json); to manipulate the object echo $receive->email; What is the problem? – Monnomcjo Jun 24 '22 at 12:22
  • Yes I can, {["firstName":"rob","lastName":"shelford","email":"design@wearenv.co.uk","telephone":"01245454545"]} I think that is what is needed, I am new to json and php. – Yenmangu Jun 24 '22 at 12:22
  • Does this answer your question? [Associative array to JSON](https://stackoverflow.com/questions/10890107/associative-array-to-json) – vinceAmstoutz Jun 24 '22 at 12:23
  • I did see that before posting but its backwards to what I want. In that instance, the user is getting what I am expecting, but not what I am getting. – Yenmangu Jun 24 '22 at 12:26
  • 1
    _{["firstName":"rob","lastName":"shelford","email":"design@wearenv.co.uk","telephone":"01245454545"]}_ This is not a valid json string. – shingo Jun 24 '22 at 12:27
  • If you want _[{"firstName":"rob","lastName":"shelford","email":"design@wearenv.co.uk","telephone":"01245454545"}]_, try `json_encode([$data]);` – shingo Jun 24 '22 at 12:28
  • I apologise if my wording is not correct, sometimes I find it difficult to articulate what is in my head. – Yenmangu Jun 24 '22 at 12:29
  • @shingo Thankyou, I think this is what I was missing!! – Yenmangu Jun 24 '22 at 12:37
  • @shingo This nearly works.. my actual live data includes a nested associative array, but using ([$data]) doesn’t put the square brackets around the nested array. How do I ensure the output is a nested array? – Yenmangu Jun 24 '22 at 13:33
  • What do your data look like? I think the result of encoding [$data] is always surrounded by square brackets. – shingo Jun 24 '22 at 14:07
  • could you reply as an answer, I dont want to get flagged as a 'discussion' in this comment thread – Yenmangu Jun 24 '22 at 14:09
  • You can update your question, add an example of your actual data and the expected result. – shingo Jun 24 '22 at 14:13
  • you can send safe the encoding(that function is meant to do that) –  Jun 24 '22 at 14:48

1 Answers1

0

If you want nested array to be encoded with square brackets surrounded, just put it in an array in php.

$data['applicants'] = [$data['applicants']];
echo json_encode($data);

FYI indexed array [1,2] will be encoded to json list [1,2], and associative array ['x'=>1,'y'=>2] will be encoded to json object {"x":1,"y":1}.

shingo
  • 18,436
  • 5
  • 23
  • 42