数组搜索,添加和删除

时间:2014-07-23 16:00:27

标签: php

我有下表:

CREATE TABLE sample (id INT AUTO_INCREMENT PRIMARY KEY, entry_id INT NOT NULL, last_change timestamp);
INSERT INTO sample (entry_id) VALUES (1), (2), (3), (4);

我的代码执行以下操作:

  • 接收POST输入并将值与上表进行比较。
  • 如果POST数据的值在entry_id列中不存在,那么它将执行INSERT语句。
  • 如果表在entry_id列中的值在POST数据中不存在,那么它将执行DELETE语句

PHP代码:

<?php
// $_POST data:
$submittedEntries = array(1, 3, 4, 5, 6);

// $dbORM->getEntries()
$currentEntries   = array(
    array("id" => 100, "entry_id" => 1, "timestamp" => "2014-07-24 2:14:00"),
    array("id" => 101, "entry_id" => 2, "timestamp" => "2014-07-24 2:14:00"),
    array("id" => 102, "entry_id" => 3, "timestamp" => "2014-07-24 2:14:00"),
    array("id" => 103, "entry_id" => 4, "timestamp" => "2014-07-24 2:14:00"),
)
$entriesToAdd     = array();
$entriesToRemove  = array();

// Find Entries to Add
// Loop through each entry submitted
foreach($submittedEntries as $entry_id) {
    $exists = false;

    // Loop through all Current Entries
    foreach($currentEntries as $existingEntry) {
        // Find matching IDs
        if ($existingEntry["entry_id"] == $entry_id) {
            $exists = true;
        }
    }

    if ($exists == false) {
        $entriesToAdd[] = $entry_id;
    }
}

// Find Entries to Remove
// Loop through all Current Entries
foreach($currentEntries as $existingEntry) {
    $remove = true;

    // Loop through each entry submitted
    foreach($submittedEntries as $entry_id) {
        // Find matching IDs
        if ($existingEntry["entry_id"] == $entry_id) {
            $remove = false;
        }
    }

    if ($remove == true) {
        $entriesToRemove[] = $existingEntry["entry_id"];
    }
}

// Add New Entries
foreach($entriesToAdd as $entry_id) {
    $dbORM->addEntry($entry_id);
}

// Remove Entries
foreach($entriesToRemove as $entry_id) {
    $dbORM->removeEntry($entry_id);
}

$entries = $dbORM->getEntries(); // SELECT * from sample;
print_r($entries);

/*
    Expected Output:

    array(
        array("id" => 100, "entry_id" => 1, "timestamp" => "2014-07-24 2:14:00"),
        array("id" => 102, "entry_id" => 3, "timestamp" => "2014-07-24 2:14:00"),
        array("id" => 103, "entry_id" => 4, "timestamp" => "2014-07-24 2:14:00"),
        array("id" => 104, "entry_id" => 5, "timestamp" => "2014-07-24 3:27:00"),
        array("id" => 105, "entry_id" => 6, "timestamp" => "2014-07-24 3:27:00")
    )
*/

有更好的方法吗?我查看了所有的php数组函数,它们似乎无法深入搜索多维数组。

2 个答案:

答案 0 :(得分:2)

如果表格必须始终与POST完全相同,那么最简单的方法是DELETE所有行,然后是INSERT所有帖子。

另一种解决方案可能是在entry_id上使用唯一键... 这样你就可以简单地做两个小查询:

$insert = "INSERT IGNORE INTO sample (entry_id)VALUES(".implode('),(',$submittedEntries).")";
$delete = "DELETE FROM sample WHERE entry_id NOT IN(".implode(',',$submittedEntries).")";

答案 1 :(得分:1)

如果要在PHP中同步这两个列表,则可以使用array_walk()运行db列表,然后使用in_array()查看它是否在提交的POST中。然后,您可以过滤到两个数组。

<?php
$entriesToAdd     = array();
$entriesToRemove  = array();

$submittedEntries = array(1, 3, 4, 5, 6);
$currentEntries   = array(
    array("id" => 100, "entry_id" => 1),
    array("id" => 101, "entry_id" => 2),
    array("id" => 102, "entry_id" => 3),
    array("id" => 103, "entry_id" => 4),
);

function AWS(&$item, $key) {
    global $submittedEntries, $entriesToAdd, $entriesToRemove;

    if(in_array($item["entry_id"], $submittedEntries))
    {
        array_push($entriesToAdd, $item);
    }
    else
    {
        array_push($entriesToRemove, $item);
    }
}
array_walk( $currentEntries, 'AWS' );

echo "Add:";
print_r($entriesToAdd);
echo "Remove:";
print_r($entriesToRemove);

输出:

Add:Array
(
    [0] => Array
        (
            [id] => 100
            [entry_id] => 1
        )

    [1] => Array
        (
            [id] => 102
            [entry_id] => 3
        )

    [2] => Array
        (
            [id] => 103
            [entry_id] => 4
        )

)
Remove:Array
(
    [0] => Array
        (
            [id] => 101
            [entry_id] => 2
        )

)