1

我正在尝试将我的数组转换为查询字符串。也许你能帮助我。

我的数组看起来像:

Array
(
    [3] => Array
        (
            [0] => 7
            [1] => 13
            [2] => 1
        )

    [4] => Array
        (
            [0] => 14
            [1] => 2
        )

)

我正在尝试将其转换为:

(FIND_IN_SET('13', vals) || FIND_IN_SET('7', vals) || FIND_IN_SET('1', vals)) AND (FIND_IN_SET('14', vals) || FIND_IN_SET('2', vals))

我怎样才能做到这一点?谢谢!

4

1 回答 1

2

这可能会对您有所帮助。

 [akshay@localhost tmp]$ cat test.php
 <?php

 $array = array(array(7,13,1),array(14,2));

 function convert_to_string($array)
 {
  return implode(" AND ",array_map(function($v){ return "(".implode(" || ", array_map( function($q){ return sprintf("FIND_IN_SET('%s', vals)",$q);},array_values($v))).")";},array_values($array)));    
 }

 echo convert_to_string($array);

 ?>

输出

 [akshay@localhost tmp]$ php test.php
 (FIND_IN_SET('7', vals) || FIND_IN_SET('13', vals) || FIND_IN_SET('1', vals)) AND (FIND_IN_SET('14', vals) || FIND_IN_SET('2', vals))
于 2015-05-15T09:39:27.797 回答