0

Here I'am using php array concept,I have a array in php just like $array=array('name1,name2');I want to convert this array into following format for some requirement $array=('name1','name2');

And please let me know,how to convert first array into second array format and also what is the difference between this two array format. Please help me...

This is what I have done so far

foreach($query->result_array() as $row) { 
    $galleries[]= $row["subjects"]; 
    $ids = join("','",$galleries); 
} 
echo $pps="SELECT * FROM student_register 
                    WHERE s_name IN ('$ids')"; 

I got output like this :

SELECT * FROM student_register 
         WHERE s_name IN ('english,malayalam,hindi') 

Instead of above I want following output

SELECT * FROM student_register 
         WHERE s_name IN ('english','malayalam','hindi')
Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • Well the second format isn't valid PHP for one thing – iainn Jul 27 '18 at 10:12
  • Did you mean $array=['name1','name2'] ? [] is an array in PHP. – MyLibary Jul 27 '18 at 10:14
  • My code is looking like, foreach($query->result_array() as $row) { $galleries[]= $row["subjects"]; $ids = join("','",$galleries); } echo $pps="SELECT * FROM student_register WHERE s_name IN ('$ids')"; I got output SELECT * FROM student_register WHERE s_name IN ('english,malayalam,hindi') Instead of aboue i want following output SELECT * FROM student_register WHERE s_name IN ('english','malayalam','hindi') – Roshima Manoharan Jul 27 '18 at 10:23
  • Try `implode()` instead of `join()` – Eng Cy Jul 27 '18 at 10:27
  • pls always response to the answers by giving some comments or ,if it helps you, by marking it as green and upvoting, it is the best way to thanks all the programmers – Pradeep Jul 28 '18 at 14:42

1 Answers1

0

Hope this will help you :

Use CI query builder's where_in to get the desired result

foreach($query->result_array() as $row) 
{ 
    $ids[] = $row["subjects"]; 
} 

$this->db->where_in('s_name', $ids);
$result = $this->db->get('student_register')->result();
//print_r($result);
echo $this->db->last_query();

For more : https://www.codeigniter.com/user_guide/database/query_builder.html

Pradeep
  • 9,667
  • 13
  • 27
  • 34