Matheos
Matheos

Reputation: 114

Array search, add and delete

I have the following table:

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);

My code does the following:

PHP Code:

<?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")
    )
*/

Is there a better way to do this? I looked into all the php array functions and they do not seem to be able to deep search of multidimensional arrays.

Upvotes: 1

Views: 58

Answers (2)

AeroX
AeroX

Reputation: 3443

If you want to compair the two lists in PHP then you can use array_walk() to run through the db list and then use in_array() to see if it's in the submitted POST. You can then filter into two arrays.

<?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);

This outputs:

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
        )

)

Upvotes: 1

Salketer
Salketer

Reputation: 15711

If the table has to always be exactly like the POST, simplest way is to DELETE all rows and then INSERT all posts.

Another solution could be the use of unique key on entry_id... This way you could really simply do 2 small queries:

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

Upvotes: 2

Related Questions