根据另一个数组从数组中删除项目

时间:2015-06-28 15:13:37

标签: javascript jquery arrays

我有两个数组:

<html>
<?php
    // declare connection variables
    $host = "localhost";
    $username = "root";
    $password = "";
    $dbname = "tsdb";
    $socket = "/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock";

    // DB Connect
    $mysqli = new MySQLi($host, $username, $password, $dbname, ini_get('mysqli.default_port'), $socket) or die ('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);

    // get URL record 'id'
    $id = $_GET['id'];

    // get result of record from 'id'
    $query = "SELECT * FROM models WHERE id =$id";
    $result = $mysqli->query($query);
    $row = $result->fetch_array();
    $result->free();
    ?>

    <!-- vertical table for record edit -->

    <div class="container-fluid">
        <div class="row">
            <div class="col-md-6">
                <h3>Edit Details</h3>

                <form class="form-group" id="editRecord" action="#" method="post">
                    <table class="table table-striped">
                        <tr>
                            <th scope="row">Record ID:</th>

                            <td><?php echo $row['id']; ?></td>
                        </tr>

                        <tr>
                            <th scope="row">Display Name:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['name']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Featured:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['featured']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Sex:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['sex']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Age:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['age']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Ethnicity:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['ethnicity']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Contact Number:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['number']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Email:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['email']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Display Email ?:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['displayEmail']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Postcode:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['postcode']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Nearest Station:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['station']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Parking:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['parking']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Shower:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['shower']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Outcalls:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['outcalls']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Price:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['price']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Heading:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['heading']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Bio:</th>

                            <td><input type='text' class='form-control' value="<?php echo $row['bio']; ?>"></td>
                        </tr>

                        <tr>
                            <th scope="row">Created:</th>

                            <td><?php echo $row['created']; ?></td>
                        </tr>

                        <tr>
                            <th scope="row">Last Updated:</th>

                            <td><?php echo $row['updated']; ?></td>
                        </tr>
                    </table>

                    <div class="btn-group">
                        <button class="btn btn-default" name="update" type="submit">Update Record</button> 
                        <button class="btn btn-default" name="delete" type="submit">Delete Record</button>
                    </div>

                </form>
            </div>
        </div>
    </div>

    <?php   

    // button execute code
    if (isset($_POST['update'])) {

        $name = $_POST['name'];
        $featured = $_POST['featured'];
        $sex = $_POST['sex'];
        $age = $_POST['age'];
        $ethnicity = $_POST['ethnicity'];
        $number = $_POST['number'];
        $email = $_POST['email'];
        $displayEmail = $_POST['displayEmail'];
        $postcode = $_POST['postcode'];
        $station = $_POST['station'];
        $parking = $_POST['parking'];
        $shower = $_POST['shower'];
        $outcalls = $_POST['outcalls'];
        $price = $_POST['price'];
        $heading = $_POST['heading'];
        $bio = $_POST['bio'];

        $updateQuery = "UPDATE models SET name=?, featured=?, sex=?, age=?, ethnicity=?, number=?, email=?, displayEmail=?, postcode=?, station=?, parking=?, shower=?, outcalls=?, price=?, heading=?, bio=? WHERE id = 'id'"; 

    $updatestmt = $mysqli->prepare($updateQuery);                    
    $updatestmt->bind_param('sssissssssssssss', $name, $featured, $sex, $age, $ethnicity, $number, $email, $displayEmail, $postcode, $station, $parking, $shower, $outcalls, $price, $heading, $bio);

    $updatestmt->execute();
    $updatestmt->close();
    }
    elseif (isset($_POST['delete'])) {
        $deleteAlert = "Feature not available!";
        echo "<script type='text/javascript'>alert('$deleteAlert');</script>";  
    };
    $mysqli->close();
    ?>
</body>
</html>

如何获得这样的数组:

var firstArr = [1,2,3,4,5];
var secondArr = [2,3];

我想使用一个解决方案,当数组的元素是具有多个属性的对象时,可以使用该解决方案。

2 个答案:

答案 0 :(得分:2)

使用filter

  

filter()方法创建一个包含所有传递元素的新数组   由提供的函数实现的测试。

例如:

firstArr.filter(function(item){ 
    return secondArr.indexOf(item) === -1; 
});

答案 1 :(得分:0)

您可以使用此功能删除项目。

function removeA(arr) {
    var what, a = arguments, L = a.length, ax;
    while (L > 1 && arr.length) {
        what = a[--L];
        while ((ax= arr.indexOf(what)) !== -1) {
            arr.splice(ax, 1);
        }
    }
    return arr;
}
var ary = ['three', 'seven', 'eleven'];
removeA(ary, 'seven');
相关问题