0

I have an array like this

Array ( 
    [14] => 0.46902846738366
    [16] => 0.40289063077504
    [17] => 0.54903658244928 
) 

14, 16, 17 are at the same array key id of my tables that are in the database. how do I can access my information from a database table based on the key array (14, 16, 17)?

we assume that in the table there is a lot of data that is stored but I just wanted to take the data with id 14, 16, 17. The selected later from the id that I will perform mathematical operations to find the value of the highest of the three.?

4
  • It's not clear what you are trying to achieve here... you want to know how to do a SQL: "SELECT * FROM table WHERE id = ???" Commented May 22, 2015 at 17:29
  • 1
    SELECT data FROM table WHERE id IN (14,16,17) Commented May 22, 2015 at 17:30
  • no, I want to use the condition where dynamically rather than manually. where the condition is based on key array I have Commented May 22, 2015 at 17:33
  • 2
    Read about array_keys and implode functions. Then substitute the result to your query. Commented May 22, 2015 at 17:36

3 Answers 3

1
$arry = array(14 => 0.46902846738366,16 => 0.40289063077504,17 =>0.54903658244928 );
$idarray=array_keys($arry);

"SELECT data FROM table WHERE id IN (".implode(',',$idarray).")";
Sign up to request clarification or add additional context in comments.

Comments

1

to get rows using ids put this code in your model(let's call it model)

function get($ids) {
return $this->db->where_in('id', $ids)->order_by->('my_col_name')->get('storage')->result(); // or result_array();
}

and call it like this

$data = $this->model->get(array_keys($zip_array));// or $this->model->get($array_a); from your last question

data in $data will be sorted from max value to low value, so automatically you got the max one too ! just need to change my_col_name to your column name

update: of course you can only select max value, if you want this, change get() to this

return $this->db->where_in('id', $ids)->select_max('my_col_name')->get('storage')->row();

Comments

0
$myArray = array ( 
    '14' => 0.46902846738366,
    '16' => 0.40289063077504,
    '17' => 0.54903658244928,
) ;

$myArrayForQuery = implode(',',array_keys($myArray));

$myQuery = 'SELECt * FROM mytable where id in ( '.$myArrayForQuery.' );';

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.