-1

I'm new to json and php,
I trying to encode arrays into JSON object.

I tried this JSON array:

<?php
    $requestJson = json_encode([
        'orderNumber'=> "502763-20171027-00006701",
        'PackageModelList'=>
        [
          (
            "basketId": 10666496,
            "SenderModel": 
            (
              "phoneNumber": "5678"
            ),
            "ItemModelList": 
            [
              (
                "itemDetailId": 10666496
              )
            ]
          ),
          (
            "basketId": 10666497,
            "SenderModel": 
            (
              "phoneNumber": "5678"
            ),
            "ItemModelList": 
            [
              (
                "itemDetailId": 10666497
              )
            ]
          )
        ]       
    ]);

?>

but this result is this.

PHP Parse error: syntax error, unexpected ':' in C:\test.php on line 7

I modfied : to =>, but same result.

PHP Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in C:\test.php on line 7

What is problem?
Thanks in advance for reply.

Phil
  • 157,677
  • 23
  • 242
  • 245
  • 1
    What are those parentheses (ie `(` and `)`) meant to be? You seem to be mixing up PHP array syntax with _something_ else – Phil Sep 17 '18 at 06:07
  • the parameter you passed is not a valid array.. – Rafee Sep 17 '18 at 06:07
  • Possible duplicate of [PHP parse/syntax errors; and how to solve them?](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – Nigel Ren Sep 17 '18 at 07:09

1 Answers1

1

Your code error because you use (, ), : in array, array in PHP use [, ], =>.

(, ), : is JSON code.

"basketId": 10666496,
"SenderModel": 
(
  "phoneNumber": "5678"
),

Its Json Code.

you can replace

  • "(" to "["
  • ")" to "]"
  • ":" to "=>"

You can try this

<?php
$requestJson = json_encode([
  'orderNumber'=> "502763-20171027-00006701",
  'PackageModelList'=>
  [
    [
      "basketId"=> 10666496,
      "SenderModel"=> 
      [
        "phoneNumber"=> "5678"
      ],
      "ItemModelList"=> 
      [
        "itemDetailId"=> 10666496
      ]
    ],
    [
      "basketId"=> 10666497,
      "SenderModel"=> 
      [
        "phoneNumber"=> "5678"
      ],
      "ItemModelList"=> 
      [
        "itemDetailId"=> 10666497
      ]
    ]
  ]       
]);

?>