2

I need to create this JSON array in PHP. If I echo this:

{"SUCCESS":[{"message":"Your Accout Info","AccoutInfo":{"credit":"$56.98USD","mail":"[email protected]","currency":"USD"}}],"apiversion":"2.3.1"}

The echo code above works as I want.


The code below building a array doesn't work:

    header('Content-type:application/json;charset=utf-8');
    $data[SUCCESS] = array(
        'credit'   => '$56.98USD',
        'mail' => '[email protected]',
        'message' => 'Your Accout Info',
        'currency' => 'USD',
        'apiversion' => '2.3.1'  
    );

    echo json_encode($data);

What am I missing here? Thanks


UPDATE:

I have updated my code to this:

    $data = array('SUCCESS' => array(
        'message'   => 'Your Accout Info',
        'AccoutInfo' => array(
        'credit' => '$56.98USD',
        'mail' => '[email protected]',
        'currency' => 'USD'),
        'apiversion' => '2.3.1')
   );

And now I get this result:

{"SUCCESS":{"message":"Your Accout Info","AccoutInfo":{"credit":"$56.98USD","mail":"[email protected]","currency":"USD"},"apiversion":"2.3.1"}}

But I need this result:

{"SUCCESS":[{"message":"Your Accout Info","AccoutInfo":{"credit":"$56.98USD","mail":"[email protected]","currency":"USD"}}],"apiversion":"2.3.1"}

Any advice to solve this?

4 Answers 4

1

change

 $data[SUCCESS] = array(...

to

 $data["SUCCESS"] = array(
Sign up to request clarification or add additional context in comments.

Comments

1

Firstly create $data, it can be an object or an array and use SUCCESS as a string 'SUCCESS' rather than an undefined constant.

header('Content-type:application/json;charset=utf-8');
$data = array('SUCCESS' => array(
        array(
            'AccoutInfo' => array(
                'credit'   => '$56.98USD',
                'mail' => '[email protected]',
                'currency' => 'USD'
            ),
            'message' => 'Your Accout Info',
        )
    ),
    'apiversion' => '2.3.1'  
);

echo json_encode($data);

also the json you hard code and the structure you try to build is very different

2 Comments

Didnt work, looks like "AccoutInfo" is a array inside array again?
Its reporting error on line 12. Anyway I got it working in my answer below. Thank you for your help. It helped me to make this array successfull. Check below. Thanks
0

Change to

$data = array('SUCCESS' => array(
        'credit'   => '$56.98USD',
        'mail' => '[email protected]',
        'message' => 'Your Accout Info',
        'currency' => 'USD',
        'apiversion' => '2.3.1'  
    )
);

echo json_encode($data);

Comments

0

Finally I got it working :-)

<?php
    header('Content-type:application/json;charset=utf-8');
    $data = Array
    (
        'SUCCESS' => Array
            (
                '0' => Array
                    (
                        'message' => 'Your Accout Info',
                        'AccoutInfo' => Array
                            (
                                'credit' => '$56.98USD',
                                'mail' => '[email protected]',
                                'currency' => 'USD'
                            )
                    )
            ),
        'apiversion' => '2.3.1'
        );

    echo json_encode($data);
?>

Thanks to @Musa who pointed me in right direction.

Comments

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.