php multidimensional array to sql
phpcn_u1582
phpcn_u1582 2017-06-14 10:50:08
0
2
924

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));
?>
phpcn_u1582
phpcn_u1582

reply all(2)
曾经蜡笔没有小新

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 = [] )

{
    $_temp_string = '';
    foreach ($arrayData as $parameterArray) {
        foreach ( $parameterArray as $parameter ) {
        if ( is_array( $parameter ) && !empty( $parameter ) ) {
            $sqlCondition = $parameter[0];
            $sqlValue     = $parameter[1];
        } else {
            $sqlCondition = '=';
            $sqlValue     = trim( $parameter );
        }

        //根据符号组合值的样式
        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($b));

?>

给我你的怀抱

The function itself continues to call itself. . Very infinite classification. Play however you want

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template