0

I have data that is a result from a query. I would like to insert some array variables of it. I have ID's then I tried to make a query and insert the query into a variable.

I have a data like these as a result of my query:

Array ( [0] => stdClass Object ( [id] => 27 [message] => {"msg_subject":"hello","msg_content":"world"} [date] => 2020-05-24 15:03:40 [parent_msg] => 0 ) ) 

now I want to insert a data inside the array object like the final result would be this:

Array ( [0] => stdClass Object ( [id] => 27 [message] => {"msg_subject":"Hooy","msg_content":"Ok ra?"} [date] => 2020-05-24 15:03:40 [parent_msg] => 0 ) [read] => 0 ) 

Here is what I've tried:

foreach($query->result_array() as $row){                
     $this->db->select('mp.*');    
     $this->db->from('message_tbl mp');
     $this->db->join('message_tbl mc', 'mc.parent_msg = mp.id');                
     $this->db->where('mc.id',$row['msg_id']);
     $data = $this->db->get()->result();
        array_push($data,$row['read']);
    }
print_r($data);

then when I printed my data Here is the output:

Array ( [0] => stdClass Object ( [id] => 27 [message] => {"msg_subject":"Hooy","msg_content":"Ok ra?"} [date] => 2020-05-24 15:03:40 [parent_msg] => 0 ) [1] => 0 ) 

1 Answer 1

2

You need to add the $row['read'] value to the result object and then push that object to your $data array. Something like this should work:

$data = array();
foreach($query->result_array() as $row){                
     $this->db->select('mp.*');    
     $this->db->from('message_tbl mp');
     $this->db->join('message_tbl mc', 'mc.parent_msg = mp.id');                
     $this->db->where('mc.id',$row['msg_id']);
     $result = $this->db->get()->result()[0];
     $result->read = $row['read'];
     array_push($data, $result);
}
print_r($data);
Sign up to request clarification or add additional context in comments.

4 Comments

It gave me something like these : Message: array_push() expects parameter 1 to be array, null given
As a side note, it's also possible that you can put another join in the original query to get the read value and not have to do an extra query for each result.
@Jerry It will make my query much complicated that's why i want to process it in PHP
@JcJohn did you add the $data = array(); statement at the beginning?

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.