1

i have a array result like this

{"data":{"NamaKecamatan":"BEJI","total":"2","laki":"1","cewe":"1"}}
{"data":{"NamaKecamatan":"BOJONGSARI","total":0,"laki":0,"cewe":0}}
{"data":{"NamaKecamatan":"CILODONG","total":"2","laki":"1","cewe":"1"}}
{"data":{"NamaKecamatan":"CIMANGGIS","total":"4","laki":"2","cewe":"2"}}

but i want to change array like this:

data: [{NamaKecamatan: "BEJI", total: "46", laki: "18", cewe: "28"},…]
0: {NamaKecamatan: "BEJI", total: "46", laki: "18", cewe: "28"}
1: {NamaKecamatan: "BOJONGSARI", total: "20", laki: "7", cewe: "13"}
2: {NamaKecamatan: "CILODONG", total: "93", laki: "48", cewe: "45"}
3: {NamaKecamatan: "CIMANGGIS", total: "96", laki: "47", cewe: "49"}
4: {NamaKecamatan: "CINERE", total: "13", laki: "7", cewe: "6"}]

this is my controller in php

public function countByKecamatan(){
  $kecamatan = $this->db->query("SELECT IDKecamatan, NamaKecamatan FROM mskecamatan ORDER BY NamaKecamatan ASC")->result_array();
  foreach ($kecamatan as $keykecamatan) {
    $result = $this->MsLaporan->dataNullByKecamatan($dateStart, $dateEnd, $kategori, $keykecamatan['IDKecamatan']);
      if ($result == NULL) {
          $row = array(
              "NamaKecamatan" => $keykecamatan['NamaKecamatan'], 
              "total" => 0, 
              "laki" => 0, 
              "cewe" => 0, 
            );
      }else{
          $row = $result[0];
        }
        $data = array(
             "data" => $row,
           );
        echo json_encode($data);
   }
}

please help me to change this data

1
  • 1
    If you're posting an array or other data structure, please include correct syntax (outer brackets, commas) to remove any ambiguity. In particular, your expected output is syntactically invalid, so it's pretty vague about what you're looking for. Commented Jun 16, 2019 at 5:02

3 Answers 3

1
$data = '[{"data":{"NamaKecamatan":"BEJI","total":"2","laki":"1","cewe":"1"}},
{"data":{"NamaKecamatan":"BOJONGSARI","total":0,"laki":0,"cewe":0}},
{"data":{"NamaKecamatan":"CILODONG","total":"2","laki":"1","cewe":"1"}},
{"data":{"NamaKecamatan":"CIMANGGIS","total":"4","laki":"2","cewe":"2"}}]';

$array_data = json_decode($data, ture);

$result = [];

foreach($array_data as $item) {
    $result['data'][] = $item['data'];
}

return json_encode($result);
Sign up to request clarification or add additional context in comments.

1 Comment

i think this help you
0

You can approach this as following

public function countByKecamatan(){
  $kecamatan = $this->db->query("SELECT IDKecamatan, NamaKecamatan FROM mskecamatan ORDER BY NamaKecamatan ASC")->result_array();
  $i = 0;
  foreach ($kecamatan as $keykecamatan) {
    $result = $this->MsLaporan->dataNullByKecamatan($dateStart, $dateEnd, $kategori, $keykecamatan['IDKecamatan']);
    if ($result == NULL) {
    $row = array(
          "NamaKecamatan" => $keykecamatan['NamaKecamatan'], 
          "total" => 0, 
          "laki" => 0, 
          "cewe" => 0, 
        );
  }else{
    $row = $result[0];
  }
  ($i > 0) ? ($data[] = $row) : ($data = array('data' => $row));
  $i++;
 }
echo json_encode($data);
}

Comments

0

The output your getting is down to 2 minor problems in the lines...

    $data = array(
         "data" => $row,
       );
    echo json_encode($data);

The first is that you are building the array with the extra level which you say you want to remove. The second line of code then echos each piece of data one at a time. You need to build up a list of all of the records and then return the full data in one go.

public function countByKecamatan(){
  // Create empty array for data
  $data = [];
  $kecamatan = $this->db->query("SELECT IDKecamatan, NamaKecamatan FROM mskecamatan ORDER BY NamaKecamatan ASC")->result_array();
  foreach ($kecamatan as $keykecamatan) {
    $result = $this->MsLaporan->dataNullByKecamatan($dateStart, $dateEnd, $kategori, $keykecamatan['IDKecamatan']);
      if ($result == NULL) {
          $row = array(
              "NamaKecamatan" => $keykecamatan['NamaKecamatan'], 
              "total" => 0, 
              "laki" => 0, 
              "cewe" => 0, 
            );
      }else{
          $row = $result[0];
      }
      // Add new row of data to data to be returned
      $data[] = $row;
   }
   // Return all of the data in JSON format
   return json_encode($data);
}

Note that this returns the value rather than echos it - you may need to change this if when you call the controller, your framework needs to encode it some other way (i.e. as part of a Response object).

Comments

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.