假设我们要将某个数组添加到多维数组中。我可以选择要添加到主数组的数组数但是99个数组呢?下面是我的代码,我可以改变$howmany来添加我需要的数组数量(最多4个数组):
function DB_SINGLE_EXE($query, array $array_par, $howmany = 0 ,$column1 = "null", $column2 = "null", $column3 = "null", $column4 = "null"){
global $host;
global $dbname;
global $username;
global $password;
global $db;
try {
$stmt = $db->prepare($query);
$stmt->execute($array_par);
}catch(PDOException $e)
{
echo "ERROR: " . $query . "<br>" . $e->getMessage();
}
if ($howmany > 0){
$rowF = $stmt->fetchAll();
if($rowF){
$hell = count($rowF);
$stack = array(array("true" , $hell));
foreach ($rowF as $row) {
switch ($howmany) {
case 1:
array_push($stack, array($row[$column1]));
break;
case 2:
array_push($stack, array($row[$column1], $row[$column2]));
break;
case 3:
array_push($stack, array($row[$column1], $row[$column2], $row[$column3]));
break;
case 4:
array_push($stack, array($row[$column1], $row[$column2], $row[$column3], $row[$column4]));
break;
}
}
}else{
$stack = array(array("false" , "0"));
}
return $stack;
}
}
如何调用函数:
$temp_query = "SELECT * FROM users WHERE status=:one";
$temp_array = array(":one" => "OK");
$results =DB_SINGLE_EXE($temp_query, $temp_array, 4, "user", "pw", "a_succ", "m_succ");
if ($results[0][0]="true"){
$username = $results[1][0];
$password = $results[1][1];
$suc = $results[1][2];
$m_suc = $results[1][3];
// Here I want to get more results but in function I just limited to 4 outputs. It might be 99 or even more and I don't want to use "select case" for 99 cases!
}
如果你使用PHP 5.6,你可以使用省略号(…)语法。如果不跳到最后。示例来自php.net:
<?php
function sum(...$numbers) {
$acc = 0;
foreach ($numbers as $n) {
$acc += $n;
}
return $acc;
}
echo sum(1, 2, 3, 4);
?>
那么你的函数就会变成两个循环。
编辑:为了使事情更简单,下面是转换后的代码
function DB_SINGLE_EXE(...$columns){
$stack = array(array("foo" , "bar"));
foreach ($rowF as $row) {
$arr=array();
foreach($columns as $col) {
$arr[]=$row[col];
}
array_push($stack, $arr);
}
return $stack;
}
DB_SINGLE_EXE("id", "name", "age");
不使用省略号也可以这样做,只需传递一个列数组:
function DB_SINGLE_EXE($columns){
$stack = array(array("foo" , "bar"));
foreach ($rowF as $row) {
$arr=array();
foreach($columns as $col) {
$arr[]=$row[col];
}
array_push($stack, $arr);
}
return $stack;
}
DB_SINGLE_EXE(array("id", "name", "age"));
我很难解释您要做的事情,所以这可能工作,也可能不工作,因为所提供的代码实际上无法进行比较。看起来您需要从SELECT *结果集中取出某些列,或者类似的东西。假设是这样,最好只修改查询以获取所需的内容,然后使用基本的fetchAll()方法。
然而,假设由于某种原因您不能这样做,看看这是否适合您。我在这里初始化了一个$rowF变量,就像我期望的那样,当你的代码运行。如果这是不正确的,这可能是错误的,但这里是:
function DB_SINGLE_EXE(array $columnsToPush) {
$rowF = array(array('col1' => 'col1val', 'col2' => 'col2val', 'cola' => 'colaval', 'colb' => 'colbval'));
$stack = array(array('foo', 'bar'));
foreach ($rowF as $row) {
$toBePushed = array();
foreach ($columnsToPush as $columnName) {
$toBePushed[] = $row[$columnName];
}
$stack[] = $toBePushed;
}
return $stack;
}
DB_SINGLE_EXE(array('col1', 'cola'));
一些完整性检查,以确保行中存在的列名是有序的,当然还有其他加固。
编辑
看到完整的功能后,虽然我对设计有些担心,但我认为您可以使用:
大大简化事情。function DB_SINGLE_EXE($query, array $array_par){
global $host;
global $dbname;
global $username;
global $password;
global $db;
try {
$stmt = $db->prepare($query);
$stmt->execute($array_par);
} catch (PDOException $e) {
echo "ERROR: " . $query . "<br>" . $e->getMessage();
// should probably return here, or do something to halt further execution
}
return $stmt->fetchAll();
}
…
$results = DB_SINGLE_EXE($temp_query, $temp_array);
if (!empty($results)) {
// wrap in foreach if appropriate
$username = $results[0]['user'];
$password = $results[0]['pw'];
$suc = $results[0]['a_succ'];
$m_suc = $results[0]['m_succ'];
}
向函数传递99个参数似乎是一个开销。为什么不能将列作为数组传入。这将允许您传入任意数量的列键(甚至超过99);)
function DB_SINGLE_EXE( $rowF, $columns ){
$columns = array_flip( $columns );
$stack = array( array( "foo" , "bar" ) );
foreach ($rowF as $row) {
array_push( $stack, array_intersect_key( $row, $columns ) );
}
return $stack;
}
$result = DB_SINGLE_EXE(array(
array(
'col1' => 'aaa1',
'col2' => 'bbb1'
),
array(
'col2' => 'bbb2',
'col3' => 'ccc2'
),
array(
'col1' => 'aaa3',
'col2' => 'bbb3',
'col3' => 'ccc3'
),
array(
'col1' => 'aaa3',
'col5' => 'eee3',
'col6' => 'fff3'
)
), array('col1', 'col2', 'col3'));