1

Im new to Json and I encountered a problem that need to be fix so that I can continue with my project.. Hope you will help me guys..

This is my Json Object response

{
 "error":false,
 "sl_summ":
    {
        "sl_desc":"PA : Savings Account",
        "tr_date":"2015-08-17",
        "actual_balance":"483.67",
        "available_balance":"483.67"
    }
}

{
  "error":false,
  "sl_summ":
    {
        "sl_desc":"PA : Savings - Cash Bond",
        "tr_date":"2015-08-28",
        "actual_balance":"10129.43",
        "available_balance":"10129.43"
    }
}

and I want to convert my following Json Object (above) to this Json array format (below)

{
  "error":false,
   "sl_summ":
    [{
        "sl_desc":"PA : Savings Account",
        "tr_date":"2015-08-17",
        "actual_balance":"483.67",
        "available_balance":"483.67"
    },
    {
        "sl_desc":"PA : Savings - Cash Bond",
        "tr_date":"2015-08-28",
        "actual_balance":"10129.43",
        "available_balance":"10129.43"
    }]
}

this is my code

for($i = 0; $i < count($user_sldtl); $i++){
   $sl_response["error"] = FALSE;
   //$sl_response["sl_summ"]["sl_brcode"] = $user_sldtl[$i][0];
   //$sl_response["sl_summ"]["sl_memid"] = $user_sldtl[$i][3];
   $sl_response["sl_summ"]["sl_desc"] = $user_sldtl[$i][7];
   $sl_response["sl_summ"]["tr_date"] = $user_sldtl[$i][10];
   $sl_response["sl_summ"]["actual_balance"] = $user_sldtl[$i][14];
   $sl_response["sl_summ"]["available_balance"] = $user_sldtl[$i][14];
   json_encode($sl_response);
   //echo json_encode($sl_response, true);
}
1
  • You should first create your object outside the loop, then inside the loop you should push your data to the sl_summ array using eg. array_push. Commented Jun 24, 2017 at 6:03

1 Answer 1

1

You can use following codes:

<?php

$sl_response = array();
$sl_response["error"] = FALSE;
$sl_response["sl_summ"] = array();

for($i = 0; $i < count($user_sldtl); $i++){
    $item = array();
    //$item["sl_brcode"] = $user_sldtl[$i][0];
    //$item["sl_memid"] = $user_sldtl[$i][3];
    $item["sl_desc"] = $user_sldtl[$i][7];
    $item["tr_date"] = $user_sldtl[$i][10];
    $item["actual_balance"] = $user_sldtl[$i][14];
    $item["available_balance"] = $user_sldtl[$i][14];

    $sl_response["sl_summ"][] = $item;
}
//json_encode($sl_response);
echo json_encode($sl_response, true);
Sign up to request clarification or add additional context in comments.

6 Comments

You're welcome. If it worked, please accept this answer. @John
It added "error": false before "sl_summ". What's your output? @John
its okie now sir @Mohammad Hamedani. I was not able to put the code $sl_response["error"] = FALSE;
sir do you know how can I fetch the json value then store it to my android sqlite?
Sorry, i don't know android and sqlite. You can ask a new question and android man answer it. @John
|

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.