如何避免PHP数组中的重复值

时间:2017-09-02 12:19:42

标签: php mysql

Array
(
    [0] => Array
        (
            [time_from] => 06:00
            [time] => 06:00-07:00
            [booking_date_from] => 2017-09-19
            [booking_date_to] => 2017-09-19
            [court_price] => 100
            [pricetype] => hour
        )

    [1] => Array
        (
            [time_from] => 06:00
            [time] => 06:00-08:00
            [booking_date_from] => 2017-09-19
            [booking_date_to] => 2017-09-19
            [court_price] => 200
            [pricetype] => hour
        )

    [2] => Array
        (
            [time_from] => 07:00
            [time] => 07:00-09:00
            [booking_date_from] => 2017-09-19
            [booking_date_to] => 2017-09-19
            [court_price] => 200
            [pricetype] => hour
        )

    [3] => Array
        (
            [time_from] => 08:00
            [time] => 08:00-09:00
            [booking_date_from] => 2017-09-20
            [booking_date_to] => 2017-09-20
            [court_price] => 100
            [pricetype] => hour
        )

    [4] => Array
        (
            [time_from] => 11:00
            [time] => 11:00-13:00
            [booking_date_from] => 2017-09-19
            [booking_date_to] => 2017-09-19
            [court_price] => 200
            [pricetype] => hour
        )

)

我的数组是这样的,我只是想避免重复日期重复值,我想要像这样放

Array
(
    [0] => Array
        (

            [time] => 11:00-13:00
            [booking_date_from] => 2017-09-19
            [booking_date_to] => 2017-09-19
            [court_price] => 100
            [pricetype] => hour
        )

    [1] => Array
        (
            [time_from] => 08:00
            [time] => 08:00-09:00
            [booking_date_from] => 2017-09-20
            [booking_date_to] => 2017-09-20
            [court_price] => 100
            [pricetype] => hour
        )
)

我该怎么做才能得到这样的结果。

2 个答案:

答案 0 :(得分:0)

您可以尝试使用array_unique()函数here。 有了这个,您可以使用foreach函数逐个读取所有列表,并从所有数组中获取一个参数,并使用它与array_unique()匹配。试试看,如果不行,请选择downvote,我将提供更多解决方案。

答案 1 :(得分:0)

现已完全测试的代码:

假设您的多维:

<?php
$arr = array(
    array(
        "time_from" => "06:00",
        "time" => "06:00-07:00",
        "booking_date_from" => "2017-09-19",
        "booking_date_to" => "2017-09-19",
        "court_price" => "100",
        "pricetype" => "hour"
    ),
    array(
        "time_from" => "06:00",
        "time" => "06:00-08:00",
        "booking_date_from" => "2017-09-19",
        "booking_date_to" => "2017-09-19",
        "court_price" => "200",
        "pricetype" => "hour"
    ),
    array(
        "time_from" => "07:00",
        "time" => "07:00-09:00",
        "booking_date_from" => "2017-09-19",
        "booking_date_to" => "2017-09-19",
        "court_price" => "200",
        "pricetype" => "hour"
    ),
    array(
        "time_from" => "08:00",
        "time" => "08:00-09:00",
        "booking_date_from" => "2017-09-20",
        "booking_date_to" => "2017-09-20",
        "court_price" => "100",
        "pricetype" => "hour"
    ),
    array(
        "time_from" => "11:00",
        "time" => "11:00-13:00",
        "booking_date_from" => "2017-09-19",
        "booking_date_to" => "2017-09-19",
        "court_price" => "200",
        "pricetype" => "hour"
    ),
);

然后删除重复的booking_date_from的完整代码:

<?php

$arr = array(
    array(
        "time_from" => "06:00",
        "time" => "06:00-07:00",
        "booking_date_from" => "2017-09-19",
        "booking_date_to" => "2017-09-19",
        "court_price" => "100",
        "pricetype" => "hour"
    ),
    array(
        "time_from" => "06:00",
        "time" => "06:00-08:00",
        "booking_date_from" => "2017-09-19",
        "booking_date_to" => "2017-09-19",
        "court_price" => "200",
        "pricetype" => "hour"
    ),
    array(
        "time_from" => "07:00",
        "time" => "07:00-09:00",
        "booking_date_from" => "2017-09-19",
        "booking_date_to" => "2017-09-19",
        "court_price" => "200",
        "pricetype" => "hour"
    ),
    array(
        "time_from" => "08:00",
        "time" => "08:00-09:00",
        "booking_date_from" => "2017-09-20",
        "booking_date_to" => "2017-09-20",
        "court_price" => "100",
        "pricetype" => "hour"
    ),
    array(
        "time_from" => "11:00",
        "time" => "11:00-13:00",
        "booking_date_from" => "2017-09-19",
        "booking_date_to" => "2017-09-19",
        "court_price" => "200",
        "pricetype" => "hour"
    ),
);

$count_real_array = count($arr); //count number of items in array(not subitems counted)

foreach ($arr as $key) {
    $data = implode(',', $key); //array converted into string and seperated using ","
    list($var1, $var2, $var3, $var4, $var5, $var6) = explode(",", $data); //the string now spilts
    $booking_date_from[] = $var3; //taking the third value(booking_date_from) from each items in $arr
}

$unique_booking_date_from = array_unique($booking_date_from); //take every unique booking_date_from

$count = count($unique_booking_date_from);
if ($count == 1) {
    $booking_date1 = "";
}
if ($count == 2) {
    $booking_date1 = $booking_date2 = "";
}
if ($count == 3) {
    $booking_date1 = $booking_date2 = $booking_date3 = "";
}
if ($count == 4) {
    $booking_date1 = $booking_date2 = $booking_date3 = $booking_date4 = "";
}//increase if you want(the more new values need more of these)

$unique_booking_date_from_implode = implode(',', $unique_booking_date_from);
if (isset($booking_date1) && isset($booking_date2) && isset($booking_date3) && isset($booking_date4)) {
    list($booking_date1, $booking_date2, $booking_date3, $booking_date4) = explode(",", $unique_booking_date_from_implode);
} elseif (isset($booking_date1) && isset($booking_date2) && isset($booking_date3)) {
    list($booking_date1, $booking_date2, $booking_date3) = explode(",", $unique_booking_date_from_implode);
} elseif (isset($booking_date1) && isset($booking_date2)) {
    list($booking_date1, $booking_date2) = explode(",", $unique_booking_date_from_implode);
} elseif (isset($booking_date1)) {
    $booking_date1 = $unique_booking_date_from_implode;
}//when increating if statements of counts increase this oppositely.

$x = 0;
$y = 0;
$z = 0;
$a = 0;
//when increasing all above then make more variables like $b, $c, etc.
if (isset($booking_date1) && isset($booking_date2) && isset($booking_date3) && isset($booking_date4)) {
    while ($x < $count_real_array) {
        if (in_array($booking_date1, $arr["$x"])) {
            break;
        }
        $x++;
    }
    while ($y < $count_real_array) {
        if (in_array($booking_date2, $arr["$y"])) {
            break;
        }
        $y++;
    }
    while ($z < $count_real_array) {
        if (in_array($booking_date3, $arr["$z"])) {
            break;
        }
        $z++;
    }
    while ($a < $count_real_array) {
        if (in_array($booking_date4, $arr["$a"])) {
            break;
        }
        $a++;
    }
} elseif (isset($booking_date1) && isset($booking_date2) && isset($booking_date3)) {
    while ($x < $count_real_array) {
        if (in_array($booking_date1, $arr["$x"])) {
            break;
        }
        $x++;
    }
    while ($y < $count_real_array) {
        if (in_array($booking_date2, $arr["$y"])) {
            break;
        }
        $y++;
    }
    while ($z < $count_real_array) {
        if (in_array($booking_date3, $arr["$z"])) {
            break;
        }
        $z++;
    }
} elseif (isset($booking_date1) && isset($booking_date2)) {
    while ($x < $count_real_array) {
        if (in_array($booking_date1, $arr["$x"])) {
            break;
        }
        $x++;
    }
    while ($y < $count_real_array) {
        if (in_array($booking_date2, $arr["$y"])) {
            break;
        }
        $y++;
    }
} elseif (isset($booking_date1)) {
    while ($x < $count_real_array) {
        if (in_array($booking_date1, $arr["$x"])) {
            break;
        }
        $x++;
    }
}//increase this too when increasing upper values

if ($y == "0") {
    unset($y);
}
if ($z == "0") {
    unset($z);
}
if ($a == "0") {
    unset($a);
}//increase this when making $b, $c, etc. Note: donot use $x(because it is first variable and can be executed in upper while loops)

if (isset($x) && isset($y) && isset($z) && isset($a)) {
    $newArr = array_merge(array($arr[$x], $arr[$y], $arr[$z], $arr[$a]));
} elseif (isset($x) && isset($y) && isset($z)) {
    $newArr = array_merge(array($arr[$x], $arr[$y], $arr[$z]));
} elseif (isset($x) && isset($y)) {
    $newArr = array_merge(array($arr[$x], $arr[$y]));
} elseif (isset($x)) {
    $newArr = array_merge(array($arr[$x]));
}//increase this when making $b, $c, etc.


print "Result in print_r function:<pre>";
print_r($newArr);
print "</pre>";

print "<br><br><br>Result in var_dump function:";

var_dump($newArr);

我对功能性php不熟悉,我总是使用面向对象,但这是一个很好的测试程序。