为什么我对数组的查询在PHP中返回一个对象数组?

为什么我对数组的查询在PHP中返回一个对象数组?,php,mysql,Php,Mysql,我试图简单地运行一个查询并在数组中获取结果: function run_query($query) { $out = ''; $db = new PDO("mysql:host=localhost;dbname=test","test","test"); $out = $db->query($query)->fetchAll(PDO::FETCH_OBJ); return $out; } 另一方面: $l_o_array = $php_functio

我试图简单地运行一个查询并在数组中获取结果:

function run_query($query)
{
    $out = '';
    $db = new PDO("mysql:host=localhost;dbname=test","test","test");
    $out = $db->query($query)->fetchAll(PDO::FETCH_OBJ);
    return $out;
}
另一方面:

$l_o_array = $php_functions->run_query('SHOW TABLES');
$temp = implode(',', $l_o_array);
结果:可捕获的致命错误:类stdClass的对象无法转换为字符串


我假设这是因为我使用了
FETCH_OBJ
,但是我使用什么来获取字符串数组呢?

这里有一种方法可以做到:

function run_query($query)
{
    $out = '';
    $db = new PDO("mysql:host=localhost;dbname=test","test","test");
    $out = $db->query($query)->fetchAll();
    return $out;
}

$results = run_query('SHOW TABLES');

$tables = array();
foreach($results as $result)
    $tables[] = $result['Tables in test']; // Note, replace "test" with your database name

$temp = implode(',', $tables);
尝试打印(R()

理解问题

<?php
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
print_r ($a);
?>


在您使用
fetchAll
时,adear的

PDO::FETCH_ASSOC
将获取数组数组。我正在尝试在函数中动态构建数组。我希望它能够处理像“Show Tables”这样简单的事情,但也能处理像“SELECT*FROM foo”这样更复杂的事情,其中foo可能有多个列和行。为了获得“字符串数组”,必须迭代结果(数组数组)并拉出相关列。如果可以使用数组数组,只需返回
fetchAll()
的结果即可。有意义吗?好吧,这就是我现在拥有的,但是元数据不适用于PDO。有什么建议吗<代码>函数run_query($query){$out='';$db=new PDO(“mysql:host=localhost;dbname=test”,“test”,“test”);$out=$db->query($query);$md=$out->result_metadata();$out=$out->fetchAll();$fields=$md->fetch_fields();$tables=array();foreach($fields作为$field)foreach($results作为$result)$tables[]=$result[$field];返回$tables;}