1

I need this result in PHP:

$pass->setJSON('{
          "formatVersion" : 1,
          "description" : "title",
          "coupon" : {
            "primaryFields" : [
              {
                "key" : "offer",
                "label" : "title",
                "value" : "50%"
              }
            ],
          }           
        }');

I try to build this with arrays:

$jsonpost = array();
$jsonpost['formatVersion'] = '1';
$jsonpost['description'] = "test";

And then convert with JSON_ENCODE:

$json = json_encode($jsonpost);
$pass->setJSON($json);

How do I set the multi level array (coupons) in PHP?

2 Answers 2

2

You need to use:

$jsonpost['coupon']['primaryFields'][] = [
'key' => 'offer',
'label' => 'title',
'value' => '50%',
];

Or if you use PHP < 5.4 you should use syntax:

$jsonpost['coupon']['primaryFields'][] = array(
   'key' => 'offer',
   'label' => 'title',
   'value' => '50%',
);

This is because primaryFields in your Json needs to be an array so extra [] as added at the end, and primaryFields is property for coupon object

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

1 Comment

no explanation, OP don't know why he has to write it like this!
0

Parse error: syntax error, unexpected '['

1 Comment

look at my edited answer, you probably use PHP < 5.4

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.