2

我想看看用 PHP 数组创建 where 语句的最佳方法

$array = array (
  0 => array (
    'host' => 'aol.com',
    'type' => 'A',
    'ip' => '64.12.89.186',
    'class' => 'IN',
    'ttl' => '2466'
  ),
  1 => array (
    'host' => 'aol.com',
    'type' => 'A',
    'ip' => '205.188.100.58',
    'class' => 'IN',
    'ttl' => '2466'
  ),
  2 => array (
    'host' => 'aol.com',
    'type' => 'A',
    'ip' => '205.188.101.58',
    'class' => 'IN',
    'ttl' => '2466'
  ),
  3 => array (
    'host' => 'aol.com',
    'type' => 'A',
    'ip' => '207.200.74.38',
    'class' => 'IN',
    'ttl' => '2466'
  ),
  4 => array (
    'host' => 'aol.com',
    'type' => 'A',
    'ip' => '64.12.79.57',
    'class' => 'IN',
    'ttl' => '2466'
  ),
  5 => array (
    'host' => 'aol.com',
    'type' => 'NS',
    'target' => 'dns-01.ns.aol.com',
    'class' => 'IN',
    'ttl' => '2466'
  ),
    '6' => 'array (',
    'host' => 'aol.com',
    'type' => 'NS',
    'target' => 'dns-07.ns.aol.com',
    'class' => 'IN',
    'ttl' => '2466'
  ),
  7 => array (
    'host' => 'aol.com',
    'type' => 'NS',
    'target' => 'dns-06.ns.aol.com',
    'class' => 'IN',
    'ttl' => '2466'
  ),
  8 => array (
    'host' => 'aol.com',
    'type' => 'NS',
    'target' => 'dns-02.ns.aol.com',
    'class' => 'IN',
    'ttl' => '2466'
  ),
  9 => array (
    'host' => 'aol.com',
    'type' => 'SOA',
    'mname' => 'dtc-ext3.edns.aol.com',
    'rname' => 'hostmaster.aol.net',
    'serial' => '352455322',
    'refresh' => '43200',
    'retry' => '180',
    'expire' => '2592000',
    'minimum-ttl' => '300',
    'class' => 'IN',
    'ttl' => '1691'
  ),
  10 => array (
    'host' => 'aol.com',
    'type' => 'MX',
    'pri' => '15',
    'target' => 'mailin-04.mx.aol.com',
    'class' => 'IN',
    'ttl' => '2466'
  ),
  11 => array (
    'host' => 'aol.com',
    'type' => 'MX',
    'pri' => '15',
    'target' => 'mailin-01.mx.aol.com',
    'class' => 'IN',
    'ttl' => '2466'
  ),
  12 => array (
    'host' => 'aol.com',
    'type' => 'MX',
    'pri' => '15',
    'target' => 'mailin-02.mx.aol.com',
    'class' => 'IN',
    'ttl' => '2466'
  ),
  13 => array (
    'host' => 'aol.com',
    'type' => 'MX',
    'pri' => '15',
    'target' => 'mailin-03.mx.aol.com',
    'class' => 'IN',
    'ttl' => '2466'
  ),
  14 => array (
    'host' => 'aol.com',
    'type' => 'TXT',
    'txt' => 'v=spf1 ptr:mx.aol.com ?all',
    'class' => 'IN',
    'ttl' => '2465'
  ),
  15 => array (
    'host' => 'aol.com',
    'type' => 'TXT',
    'txt' => 'spf2.0/pra ptr:mx.aol.com ?all',
    'class' => 'IN',
    'ttl' => '2465'
  )
);

这是我的数组,我想在 EX type = NS 上创建一个 where 语句,依此类推,然后将所有内容传递给要显示的变量。

请注意,如果我不回复评论/问题,我将在接下来的几个小时内关闭我的互联网进行维修。

4

3 回答 3

1

你在使用dns_get_record吗?如果是这样,第二个参数会限制返回的记录类型,因此您可以使用它DNS_NS来过滤您的示例。否则使用array_filter

$wantedType = 'NS';

var_dump(array_filter($records, function($record) use ($wantedType) {
    return ($record['type'] === $wantedType);
}));

您可以进一步概括这一点:

// expand as necessary
$requirements = array(
    'type' => 'NS',
);

var_dump(array_filter($records, function($record) use ($requirements) {
    // only include if all the requirements match
    return (array_intersect($requirements, $record) == $requirements);
}));

或按类型分组:

$groupedRecords = array();

foreach ($records as $record) {
    if (!array_key_exists($record['type'], $groupedRecords)) {
        $groupedRecords[$record['type']] = array();
    }

    $groupedRecords[$record['type']][] = $record;
}

// $groupedRecords now contains all records nested by record type
于 2012-04-04T12:42:19.930 回答
0
$param = array();
$content = array();
foreach ($mainArray as $item) {
  foreach ($item as $key=>$value) {
   $params[] = "AND $key = ?";
   $content[] = $value;
  }
}

$sql = "SELECT .... WHERE " . implode($params, ' ');
$statement = // Create prepared statement with $sql
execute($statement, $content);
于 2012-04-04T12:36:06.423 回答
0

如果您可以通过 DAL 访问准备好的语句,我会选择 Churk 的解决方案。否则,我经常发现自己在构建查询时看起来像这样(使用您的 $array 格式):

foreach ( $array as $query_key => $conditions ) {
  $query = array();
  $query[] = 'select * from XXX';
  $query[] = 'where ((1=1)';
  foreach ( $conditions as $key => $value ) {
    $query[] = 'and ' . $key . ' = \'' . $value '\''; 
  }
  $query[] = ')';
  $query = implode(PHP_EOL, $query);
  echo $query, PHP_EOL;
}

这里的“技巧”是以“(1 = 1)”开头的where子句,这始终是正确的,然后所有以下相等条件都可以以“AND ...”开头。

干杯

于 2012-04-04T14:05:50.687 回答