#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    How to use json_encode


    Hi,

    i have an array with two values. I want to encode the array using json_encode in format:

    {"value1":"val","data":["name", "email"]}

    where value 1 is a paremeter in php and data is an array. But if i use json_encode it gives me different output

    here is my code:

    $arr['value1'] = 'val';
    $arr['data'] = $data //array
    json_encode($arr);

    it gives me

    {"value1":"val1","data":{"name", "email"}}



    can any one tell men how to encode the array in above format??

    thanks
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    Originally Posted by new_member
    it gives me

    {"value1":"val1","data":{"name", "email"}}
    I highly doubt that, because this isn't even valid JSON.

    Please show us the actual output.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0
    [QUOTE=Jacques1]Hi,
    {"function":"value","data":{"Name":"test","email":"test@yahoo.com"}}

    however i want to get the output

    {"function":"value","data":["test","test@yahoo.com"]}

    The array of data with name and email is passed through ajax and get through $_GET. But i dont want to get the array in format

    $data['name'] = "abc"

    rather i want to get it without name and email lablel

    as data = array('abc','test@yahoo.com')

    so that i can easily encode it. Whats the best possible way to get array in this format before encoding??
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by new_member
    Whats the best possible way to get array in this format before encoding??
    By creating it:

    PHP Code:
    $new_data = array($data['Name'], $data['email']); 
    Theoretically, you could also use array_values($data) and rely on the internal order of the array values, but I'm not sure if that's a good idea.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by Jacques1
    By creating it:

    PHP Code:
    $new_data = array($data['Name'], $data['email']); 
    Theoretically, you could also use array_values($data) and rely on the internal order of the array values, but I'm not sure if that's a good idea.
    great thanks

IMN logo majestic logo threadwatch logo seochat tools logo