php |将数组转换为 FIND_IN_SET

php | Convert array to FIND_IN_SET

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

我的数组看起来像:

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

我该怎么做?谢谢!

这可能对您有所帮助。

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