开发者

PHP appending an array

I have some code that essentially does what I want but I always like to make it simpler and more efficient whenever possible so here I am.

Basically, I have a list of pages that each run an SQL query and pull X number of relevant records, appending each one to the former by saving to a text file that's then reopened for further processing, like so:

foreach($pages as $page){

$fp = fopen("$page.txt", 'w');fclose ($fp);

$result = $db->query("SELECT X, Y, Z FROM Table WHERE Pages LIKE '%$page%'");
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {

$v = $row['X'].'|'.$row['Y'].'|'.$row['Z'].';';

file_put_contents("$page.txt", $v, FILE_APPEND);

}

$data = file_get_contents("$page.txt");
$data = explode(';', $data);

}

Now while that gets the job done I was wondering what would be the better way of doing it, I've tried messing around with mixed results, was hoping this would work but it generates an error:

foreach($pages as $page){

$result = $db->query("SELECT X, Y, Z FROM Table WHERE Pages LIKE '%$page%'");
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {

$v = $row['X'].'|'.$开发者_如何学Gorow['Y'].'|'.$row['Z'].';' . $v;

$r = array("$page"=>"$v"); 

}

$data = join($r["$page"]);
$data = explode(';', $data);

}

Any ideas? Thanks


Your code's rather inefficient. You open a file and immediately close it, which is to create the file, I presume. You then use file_get_contents() to output to that file in append mode for every iteration of the loop. That means PHP has to re-open the file, jump to the end, write some text, then close it again. It's like going to the grocery store, buying something, driving home, going back to the store, buying another item, driving home, etc... A major waste of time.

Instead, try something like this:

foreach ($pages as $page) {
    $result = $db->query(...) or die('db error...');
    $data = array();
    while($row = $result->fetchArray(...)) {
        $data[] = $row; // append the fetched row to the $data array
    }

    ... do something with $data here ...
}

After each while() loop completes, $data will be array that looks something like:

$data = array(
    0 => array('X' => 'someval', 'Y' => 'otherval', 'Z' => 'otherotherval'),
    1 => array('X' => etc.....),
    etc...
)

if you need to write this $data array out to a file for processing elsewhere, you can use serialize() to output it as a string which can be trivially converted back to a native PHP array using unserialize().


You could also do it like:

$data = array();
foreach($pages as $page){
    $result = $db->query("SELECT CONCAT(X, '|', Y, '|', Z) as `data` FROM Table WHERE Pages LIKE '%$page%'");

    while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
        $data[] = $row['data'];
    }
}

Maybe a bit faster.


If I understood what you are trying to do correctly, then yes, there are much easier ways:

$data = array();
foreach($pages as $page){
    $result = $db->query("SELECT X, Y, Z FROM Table WHERE Pages LIKE '%$page%'");

    while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
        $data[] = $row['X'].'|'.$row['Y'].'|'.$row['Z'];
    }
}

And if I misunderstood, then I am sorry.

Edit
Fixed a little bug with the declaration of the $data array


What's you error?

I can see possible problems here

$v = $row['X'].'|'.$row['Y'].'|'.$row['Z'].';' . $v;

$r = array("$page"=>"$v");

remove the quote codes on $v, and define $v as a empty variable before the loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜