开发者

Making a string out of array of arrays

开发者 https://www.devze.com 2023-04-10 00:37 出处:网络
Sorry for asking probably a basic question, but I\'m having a blackout. If I have an array of arrays: Array

Sorry for asking probably a basic question, but I'm having a blackout.

If I have an array of arrays:

Array
(
    [appsversion] => Array
        (
            [0] => appsversion='6.1.0.33'
            [1] => appsversion='6.1.0.40'
        ),
    [osversion] => Array
        (
            [0] => osversion='6.1.0.53'
            [1] => osversion='6.1.0.42'
        )
)

how do I please construct an SQL condition with OR and AND of it?

I.e. for something like:

    $condition = '';
    foreach ($CONDITIONS as $key => $value) {
        # XXX I'm so lost here XXX
    }

    $sql = sprintf('select %s from mytable where %s', $condition);
    $sth = $pg->prepare($sql);
    $sth->execute();

I need the constructed string

    (appsversion='6.1.0.33' OR appsversion='6.1.0.40') AND
    (osversi开发者_如何学编程on='6.1.0.53' OR osversion='6.1.0.42') 

or:

    appsversion in ('6.1.0.33', '6.1.0.40') AND
    osversion in ('6.1.0.53', '6.1.0.42') 

Please give me few hints - to get my brain started again :-)


There:

$where = array();
foreach ($conditions as $values) {
    $where[] = '('.implode(' OR ', $values).')';
}
$string = implode(' AND ', $where);

Yields:

(appsversion='6.1.0.33' OR appsversion='6.1.0.40') AND (osversion='6.1.0.53' OR osversion='6.1.0.42')


You mean something like this?

$condition = "(appsversion = '" . implode("' OR appsversion = '", $conditions['appsversion']) . "')";
$condition .= " AND (osversion = '" . implode("' OR osversion = '", $conditions['osversion']) . "')";

Or, cleaner with IN:

$condition = "appsversion IN ('" . implode("', '", $conditions['appsversion']) . "')";
$condition .= " AND osversion IN ('" . implode("', '", $conditions['osversion']) . "')";


In case your original array does not duplicate the key names and provides values "as-is" (I somehow misread your question, but probably it's useful):

$conditions = array(
    'appsversion' => Array('6.1.0.33', '6.1.0.40'),
    'osversion' => Array('6.1.0.53', '6.1.0.42'),
);

foreach ($conditions as $key => &$values) {
    foreach($values as &$value)
        $value = sprintf("%s='%s'", $key, $value);
    $values = sprintf("(%s)", implode(' OR ', $values));
}
unset($values, $value);
echo implode(" AND \n", $conditions);

Output/Demo:

(appsversion='6.1.0.33' OR appsversion='6.1.0.40') AND (osversion='6.1.0.53' OR osversion='6.1.0.42')


$conditions1 = array();
foreach ($CONDITIONS['appsversion'] as $value) {
    $conditions1[] = sprintf("appversion = '%s'", mysql_real_escape_string($value));
}

$conditions2 = array();
foreach ($CONDITIONS['osversion'] as $value) {
    $conditions1[] = sprintf("osversion = '%s'", mysql_real_escape_string($value));
}

$sql = sprintf('select %s from mytable where (%s) AND (%s)', $cols, implode(' OR ', $conditions1), implode(' OR ', $conditions2));
$sth = $pg->prepare($sql);
$sth->execute();


foreach($C as $key => $value)
{
    $str .= ($str ? ' AND ' : '') . '(' . implode(' OR ',$value) . ')';
}
0

精彩评论

暂无评论...
验证码 换一张
取 消