2

I would like to see the best way to create a where statement with a PHP array

$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'
  )
);

This is my Array I would like to make a where statement for on EX type = NS and so on then pass all that in to a variable to be displayed.

Please note if I do not respond to comment/questions, I will with in the next few hours my internet will be going down for repair.

4

3 に答える 3

1

dns_get_recordを使用していますか? その場合、2 番目のパラメーターは返されるレコードの種類を制限するため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を介して準備済みステートメントにアクセスできる場合は、チャークのソリューションを使用します。それ以外の場合、クエリの作成でよく行うことは次のようになります ($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;
}

ここでの「トリック」は、where 句を「(1=1)」で開始することです。これは常に true であり、その後のすべての等価条件は「AND ...」で開始できます。

乾杯

于 2012-04-04T14:05:50.687 に答える