-1

code:

<?php
    if(!isset($candidate_id))
    {
        header("location:".base_url()."login");
    }
    $this->db->select('event,event_title,description,s_date');
    $this->db->from('event');
    $this->db->where('candidate_id',$cid);
    $this->db->order_by('s_date','desc');
    $query = $this->db->get();
    if($query->num_rows() > 0)
    {
        $result = $query->result_array();
        $record = array();
        foreach($result as $row)
        {
            $record[] = $row;
        }
        echo json_encode($record,JSON_NUMERIC_CHECK);
    }
    else
    {
        $this->session->set_flashdata('no_event',"<p>No event Added</p>");
    }
?>

current output:

[{"event":"2019-03-06","event_title":"meeting","description":"meeting with xyz","s_date":"2019-03-04"}]

expected output:

[{event:"2019-03-06",event_title:"meeting",description:"meeting with xyz",s_date:"2019-03-04"}]

I am creating a simple JSON API using json_encode() function. Now, API I created successfully but the output is unexpected as I mention above. Now, What I actually want I also mention above. So, How can get I expected output? Please help me.

Thank You

steave
  • 135
  • 2
  • 15
  • 2
    your expected output is **not JSON** - whichever software you need it for, if you have access to it - fix it! if you don't have access, tell whoever has to fix it. – Franz Gleichmann Mar 04 '19 at 06:13

2 Answers2

1

The problem is, if you remove the quotes from the key, it's no longer JSON. I'm assuming you want it similar to a JavaScript object, but that should be done on the client side, using JSON.parse().

ie:

var apiResponse = '[{"event":"2019-03-06","event_title":"meeting","description":"meeting with xyz","s_date":"2019-03-04"}]';

var json = JSON.parse(apiResponse);

console.log(json);
console.log(json[0].description);
Blue
  • 22,608
  • 7
  • 62
  • 92
-1

Try this

$array_final = preg_replace('/"([a-zA-Z_]+[a-zA-Z0-9_]*)":/','$1:',json_encode($you-array));

Output

{event:"2019-03-06",event_title:"meeting",description:"meeting with xyz",s_date:"2019-03-04"} 
Danish Ali
  • 2,354
  • 3
  • 15
  • 26