3

I have an array that includes the following data:

array(3) { [0]=> array(1) { ["project_id"]=> string(2) "14" } [1]=> array(1) { ["project_id"]=> string(2) "21" } [2]=> array(1) { ["project_id"]=> string(2) "13" } }

I need to echo return all rows from my database where the project ids are equal to the ones in my array.

Using the below code how can I only get the records where the ids equal those in the database???

    function get_projects($id){
        $data = '';

        $this->db->where('id', $id); //HOW TO GET MULTIPLE IDS
        $query = $this->db->get('projects'); 

        foreach ($query->result() as $row) {
            $data[] = array(
                'id' => $row->id,
                'user_id' => $row->user_id,
                'project_name' => $row->project_name,
            );
        }

        return $data;

    }

1 Answer 1

4

Use where_in();

$names = array('Frank', 'Todd', 'James');
$this->db->where_in('username', $names);
// Produces: WHERE username IN ('Frank', 'Todd', 'James')

See the user guide for more.

Sign up to request clarification or add additional context in comments.

4 Comments

How can I get the data out of the multidimensional array and into a standard array like in your answer???
A simple foreach loop should make it easy to get the values from the multi-dimensional array and put it into a flat array.
Can you help me to do it or should I open a new question, have searched but cannot find the answer to suit my needs...
I'm in and out of here today so I would recommend asking a new question. Make sure you show some code your tried in your question or else you'll get voted down. That happens a lot with looping questions. They like to see an effort first before offering help. And if you do post a new question post the link here so I can find it and try to get to it if i can.

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.