Home  >  Q&A  >  body text

php multidimensional array to sql

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_u1582phpcn_u15822708 days ago919

reply all(2)I'll reply

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-06-14 10:52:08

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

    ?>

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-06-14 10:52:08

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

    reply
    0
  • Cancelreply