Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP内爆值_Php_Implode - Fatal编程技术网

PHP内爆值

PHP内爆值,php,implode,Php,Implode,如何从下面的数组中获取单个值?第19行的当前结果类似于重复两次的19 E:/database/users/1919/ <?php $db = new SQLite3('C:/inetpub/wwwroot/database.db'); $row = $db->query('select id from users'); while ($lastrow = $row->fetchArray()) { $id = implode($lastrow); } $database = '

如何从下面的数组中获取单个值?第19行的当前结果类似于重复两次的19

E:/database/users/1919/

<?php
$db = new SQLite3('C:/inetpub/wwwroot/database.db');
$row = $db->query('select id from users');
while ($lastrow = $row->fetchArray())
{
$id = implode($lastrow);
}
$database = 'E:/database/users/'.$id.'/';
?>
我只需要19,所以URL看起来像

E:/database/users/19/

<?php
$db = new SQLite3('C:/inetpub/wwwroot/database.db');
$row = $db->query('select id from users');
while ($lastrow = $row->fetchArray())
{
$id = implode($lastrow);
}
$database = 'E:/database/users/'.$id.'/';
?>

默认情况下使用
SQLITE3\u BOTH
模式,该模式返回所选列的0索引关联数组

这意味着您将收到如下值:

$lastrow = array(
   0 => '19',
   'id' => '19'
);
要解决此问题,只需更改
fetchArray
即可使用
SQLITE3\u NUM
SQLITE3\u ASSOC
检索单个值

$row = $db->query('select id from users');
while ($lastrow = $row->fetchArray(SQLITE3_NUM)) {
    $id = implode($lastrow);
    //list($id) = $lastrow; //alternative to implode
}
$database = 'E:/database/users/'.$id.'/';
或者,您只需要指定要检索的列名,而不需要使用
内爆

$row = $db->query('select id from users');
while ($lastrow = $row->fetchArray()) {
    $id = $lastrow['id'];
}
$database = 'E:/database/users/'.$id.'/';

由于您似乎只需要最后一行,因此我建议更改查询以提高总体性能

SELECT MAX(id) FROM users 

允许您检索最后一个id,而无需遍历整个users表

if ($row = $db->query('SELECT MAX(id) FROM users')) {
    if ($row->numColumns()) {
        list($id) = $row->fetchArray(SQLITE3_NUM);
        $database = 'E:/database/users/'.$id.'/';
    }
}

由于
$lastrow
的值是正确的数组,并且您说过它会得到两次值,因此您可以尝试这样做,声明
$id=array()
并尝试将值重新分配给
$id=array\u shift($lastrow)
然后是另一个处理该值的变量,如
$inpodedarray=inpode($id)
并设置为您喜欢的
$database='E:/database/users/'。$inpulodedarray'/'这应该得到值
19
。这是一个操作数组的想法。

是否要为每个id创建URL?我可以确认这对我有效。但必须以@fyrye作为答案。非常感谢。