encoding nested json objects in php

I have a json code in JAVA, I would like to know its equivalent code in PHP.

    JSONArray objArr = new JSONArray();
    PrintWriter out = response.getWriter();
    for(int i =0 ;i<4;i++)
    {
        JSONObject obj = new JSONObject();
        obj.put("name", "punith"+i);
        obj.put("id", "123"+i);
        System.out.println(obj);
        objArr.add(obj);
    }
    out.println(objArr);
    JSONObject result = new JSONObject();
    result.put("result",objArr);
    out.println("<br>");
    out.println(result);
    JSONObject obj = new JSONObject();
    objArr = (JSONArray) result.get("result");
    obj = (JSONObject) objArr.get(0);
    out.println("<br>");
O/P of this code is :: 

{"result":[{"name":"punith0","id":"1230"},{"name":"punith1","id":"1231"},{"name":"punith2","id":"1232"},{"name":"punith3","id":"1233"}]}

I would like to get the same result with php code. Someone please help me.

Answers


Here you are

$resultArray = array();
for($i=0;$i<4;$i++){
    $element = array("name"=>"punith$i", "id"=>"123$i");
    array_push($resultArray, $element);
}
echo json_encode(array("result"=>$resultArray));

In php you have

json_encode($object);

Which stringify the object


Need Your Help

How to get the proper bounds of a view after transformation Matrix

android

I have a custom view where I use transformations. So far so good, function like setRotationY(), setScaleX(), setTranslationY() or even getMatrix() work as expected, I’m able manipulate my view and it

Android Debug Keytool Error

android keytool

I can't seem to get the SHA1 string from running the keytool command. When I am prompted for the password for my debug key I enter "android" and after hitting enter I get a string of weird characte...