数组到where语句

时间:2012-04-04 12:26:58

标签: php arrays

我希望看到用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语句,然后将所有内容传递给要显示的变量。

请注意,如果我没有回复评论/问题,我将在接下来的几个小时内将我的互联网停止维修。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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);

答案 2 :(得分: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子句,它始终为true,然后所有以下相等条件都可以'AND ...'开头。

干杯