-1

In php how get values of activity_id_0 and student_phone_0 and so on. My json string is like

array(
    'json_text' => '{
    "student_activity":{"activity_id_0":"1","student_id_0":"1","charges_0":"123"},
    "student_contact_info":{"student_phone_0":"7867857986","student_id_0":"1","student_email_0":""}
    }'
)
vnnogile
  • 143
  • 2
  • 4
  • 12

3 Answers3

0

Use json_decode():

$arr = array(
    'json_text' => '{
        "student_activity":{"activity_id_0":"1","student_id_0":"1","charges_0":"123"},
        "student_contact_info":{"student_phone_0":"7867857986","student_id_0":"1","student_email_0":""}
    }'
);

var_dump(json_decode($arr['json_text'])->student_contact_info->student_phone_0);

More info in the PHP Manual: http://php.net/manual/en/function.json-decode.php

PiranhaGeorge
  • 999
  • 7
  • 13
0

You can decode this json as array and get it by keys:

$a = array(
    'json_text' => '{
    "student_activity":{"activity_id_0":"1","student_id_0":"1","charges_0":"123"},
    "student_contact_info":{"student_phone_0":"7867857986","student_id_0":"1","student_email_0":""}
    }'
);
$json = json_decode($a['json_text'],true);
$activity_id_0 = $json["student_activity"]["activity_id_0"];
$student_phone_0 = $json["student_contact_info"]["student_phone_0"];
echo $activity_id_0,' ',$student_phone_0; //1 7867857986 
n-dru
  • 9,285
  • 2
  • 29
  • 42
0

Here you need to convert json object into an array, try below code

$info = array(
        'json_text' => '{
        "student_activity":{"activity_id_0":"1","student_id_0":"1","charges_0":"123"},
        "student_contact_info":{"student_phone_0":"7867857986","student_id_0":"1","student_email_0":""}
        }'
    );

    // here we can convert json object into an array
    $arrayInfo = json_decode($info['json_text'], true);

    echo '<pre>';
    print_r($arrayInfo['student_activity']);
    print_r($arrayInfo['student_contact_info']);

I think it will helpful for you.. :-)

sathish kumar
  • 116
  • 1
  • 8