I am writing the QueryBuilder class for building queries to MySQL, the builder accepts an array with search parameters.

I want the keyword say 'order by' to get the value, in whatever register it is not passed by the user though 'OrDeR By' .

Is there any method that can do this? Or who faced this problem prompt.

UPDATE Example input array:

 arra1 = array( 'id' => array('>=', 1), 'date' => array('<', time()), 'order by' => array('id', 'DESC'), 'limit' => array(5, 10), ); arra2 = array( 'id' => array('>=', 1), 'date' => array('<', time()), 'ORDER BY' => array('id', 'DESC'), 'LIMIT' => array(5, 10), ); 

How to make one and the first masiv work the same way. It is impossible to drive all the fields into lower case. The rest of the keys except order by & limit table fields.

  • one
    strtolower help? - Den
  • 2
    And how is the value stored in the array? Write the key to the array always, for example, in lower case. Then, when searching, you just need to bring all the parameters to lower case and that's it - Chubatiy
  • Registry is a registry. Register rather case. But it is better to remove the corresponding label. - D-side
  • one
    And what about running into lower case table names can hurt? Is SQL case-sensitive field names? - Chubatiy
  • @chubatiy is actually not when someone will not write the fields of the table, indicate the fields of the class, and the class knows which field of the model belongs to which field of the table. - Makarenko_I_V

2 answers 2

 $a = [ 'a'=>'0', 'A'=>'1', 'b'=>'2', 'ZyX'=>'3', 'HELLO'=>'4', 'Я'=>'5', ]; $arrayKeys = array_keys($a); $keyMap = array_combine($arrayKeys, $arrayKeys); $tmpArray = array_change_key_case($keyMap, CASE_LOWER); unset($a[$tmpArray['hello']]); unset($a[$tmpArray['zyx']]); unset($a[$tmpArray['a']]); 

You can try it. But there are two points.

  1. Duplicates in different registers smorzhatsya
  2. Cyrillic keys handle incorrectly.

But I think the idea itself is understandable, although if class names are taken, then I think this is not a problem.

  • Thanks for what you need, I hope it works smartly. - Makarenko_I_V

Probably so

 $testArray = array('KeYmLoVer' => 1); function getArrayValue($key,$array) { foreach($testArray as $_key=>$_val) { if(strtolower($_key) == strtolower($key)) { return $_val; } } } echo getArrayValue('keymlover',$array); 

But it is better, just go through the array and rewrite the keys in lowercase.

  • Read carefully, I said that all parameters except limit and order by are table fields and they need to save the register! - Makarenko_I_V