5

I have a json string i which has a object in one one there is one json array & another object i want to first the json array then convert into php array & other json object into the php variable.Please tell me how to do this. I have stdclass but i am not able to get accurate data.

Json String

{
    "data": [
    {
        "ques_name": "no",
        "ques_id": "1"
    }, {
        "ques_name": "yes",
        "ques_id": "2"
    }, {
        "ques_name": "no",
        "ques_id": "3"
    }, {
        "ques_name": "yes",
        "ques_id": "4"
    }, {
        "ques_name": "no",
        "ques_id": "5"
    }, {
        "ques_name": "yes",
        "ques_id": "6"
    }, {
        "ques_name": "no",
        "ques_id": "7"
    }
    ],
    "UserId": 163
}

I used following code to get the array but it gives me array of size 14 where as size should be 7

    $params=$_GET['params'];
    $arr=array();
    $decode=json_decode($params);
    $arr=$decode->data;
    print_r($arr);
1
  • What exactly does that print_r give you? Commented Feb 2, 2015 at 12:36

4 Answers 4

13

json_decode($array) will convert your json object into an array.

Edit:
you can try json_decode($array, true);. That way returned objects will be converted into associative arrays.

Edit2: using my code in edit section (json_decode($array, true);), i get the following array (which seems ok to me):

Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [ques_name] => no
                    [ques_id] => 1
                )

            [1] => Array
                (
                    [ques_name] => yes
                    [ques_id] => 2
                )

            [2] => Array
                (
                    [ques_name] => no
                    [ques_id] => 3
                )

            [3] => Array
                (
                    [ques_name] => yes
                    [ques_id] => 4
                )

            [4] => Array
                (
                    [ques_name] => no
                    [ques_id] => 5
                )

            [5] => Array
                (
                    [ques_name] => yes
                    [ques_id] => 6
                )

            [6] => Array
                (
                    [ques_name] => no
                    [ques_id] => 7
                )

        )

    [UserId] => 163
)

Edit3: for what you ask on how to get the id/name part of the array, here is a small example code:

$jsonData= ''; // put here your json object
$arrayData = json_decode($jsonData, true);
if (isset($arrayData['data']))
{
foreach ($arrayData['data'] as $data)
{
echo 'id='.$data['ques_id'].', name='.$data['ques_name'].'<br>';
}
}
Sign up to request clarification or add additional context in comments.

6 Comments

I know but it give me object of std class
I used following code to get the array but it gives me array of size 14 where as size should be 7
@TarunSharma what issue you are having in accessing it with object ?
@TarunSharma check my edit2 please and tell me if this is what you expect
it only print with print_r how can i access the first array which icntains id & name & save rhem in array
|
1

You can also try:

array = get_object_vars(jsonData)

As per http://php.net/manual/en/function.get-object-vars.php:

Returns an associative array of defined object accessible non-static properties for the specified object in scope. If a property has not been assigned a value, it will be returned with a NULL value.

1 Comment

Especially useful when you want to keep the whole JSON in object style and convert only parts of it to arrays.
0

There are lots of ways to achieve the same some of them are below

$array =  (array) json_decode($xml_variable);

From http://www.php.net/manual/en/language.types.array.php

$array = json_decode(json_encode($xml_varible), true);

or

function object_to_array(json_decode($xml_varible))
{
    if (is_array($data) || is_object($data))
    {
        $result = array();
        foreach ($data as $key => $value)
        {
            $result[$key] = object_to_array($value);
        }
        return $result;
    }
    return $data;
}

or

function object_to_array(json_decode($xml_varible)) 
{
if ((! is_array($data)) and (! is_object($data))) return 'xxx'; //$data;

$result = array();

$data = (array) $data;
foreach ($data as $key => $value) {
    if (is_object($value)) $value = (array) $value;
    if (is_array($value)) 
    $result[$key] = object_to_array($value);
    else
        $result[$key] = $value;
}

return $result;
}

Comments

0
    $phpArray = array_map(function($value,$key){
        return ['key' => $key, 'value' => $value];
    },$jsonObject,array_keys($jsonObject));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.