Convert multi-dimensional array to sql statement. The one-dimensional one has been implemented. But I don’t know how to do the two-dimensional one. Please tell me, I have many ifs now, one if for one dimension and one if for multi-dimensional. The program efficiency is good. It's low, so I want to improve the program, but it's stuck here. . .
<?php
function arrayToSqlStatement( $arrayData = [] )
{
$_temp_string = '';
foreach ( $arrayData as $k => $v ) {
if ( is_array( $v ) && !empty( $v ) ) {
$sqlCondition = $v[0];
$sqlValue = $v[1];
} else {
$sqlCondition = '=';
$sqlValue = trim( $v );
}
//根据符号组合值的样式
if ( $sqlCondition == 'LIKE' ) {
$_temp_string .= ' AND ' . $k . ' LIKE "%' . $sqlValue . '%" ';
} elseif ( $sqlCondition == 'IN' ) {
$_temp_string .= ' AND ' . $k . ' IN(' . $sqlValue . ')';
} elseif ( $sqlCondition == 'BETWEEN' ) {
$_between_value = explode( '|', $sqlValue );
$_temp_string .= ' AND ' . $k . '>="' . $_between_value[0] . '" AND ' . $k . '<="' . $_between_value[1] . '" ';
} else {
$_temp_string .= ' AND ' . $k . $sqlCondition . ' "' . $sqlValue . '" ';
}
}
return $_temp_string;
}
//第二种情况,出问题了。
$a=[
[
'bbb'=>'222',
'ccc'=>'333',
'ddd'=>['>=','444'],
'eee'=>['BETWEEN','555|666']
],
[
'bbb'=>'222',
'ccc'=>'333',
'ddd'=>['>=','444'],
'eee'=>['BETWEEN','555|666']
]
];
//第一种情况,已经实现了
$b=[
'bbb'=>'222',
'ccc'=>'333',
'ddd'=>['>=','444'],
'eee'=>['BETWEEN','555|666']
];
print_r(arrayToSqlStatement($a));
?>
The questioner wants to realize that the two-dimensional array is combined into a sql statement. To change the idea, the passed parameters are unified into multi-dimensional arrays.
<?php
function arrayToSqlStatement( $arrayData = [] )
?>
The function itself continues to call itself. . Very infinite classification. Play however you want