2

the following works (array is filled in by another MySQL query):

$array=(1,4,5,6,8);
$query=SELECT * FROM table WHERE Id IN(".implode(",",$array)."

But how can I do the following?:

$array=(
array('Id'=>1, 'Detail1'=>$row['Detail1'], 'Detail2'=>$row['Detail2']),
array('Id'=>4, 'Detail1'=>$row['Detail1'], 'Detail2'=>$row['Detail2']),
array('Id'=>5, 'Detail1'=>$row['Detail1'], 'Detail2'=>$row['Detail2']),
array('Id'=>6, 'Detail1'=>$row['Detail1'], 'Detail2'=>$row['Detail2']),
array('Id'=>8, 'Detail1'=>$row['Detail1'], 'Detail2'=>$row['Detail2']),
);
$query=SELECT * FROM table WHERE Id IN(".implode(",",$array[]['Id'])."

So i need to get all the id's, how can I do that in this multidimensional array?

Thanks!

2 Answers 2

6

I would just do something like this:

$id_list = array();

foreach($array as $item) {
 $id_list[] = $item['Id'];
}

$query = "SELECT * FROM table WHERE Id IN(".implode(',',$id_list).")";
Sign up to request clarification or add additional context in comments.

Comments

1

Another option:

$list = array_reduce($id_list,function($total,$cur_value){
     if(!is_null($total)) $total .= ',';
     return $total.$cur_value['id'];
  });
$query = "SELECT * FROM table WHERE Id IN(".$list.")";

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.