简体   繁体   English

添加前检查php多维数组中是否存在值

[英]Check if value exists in php multi-dimensional array before adding

I have a separate set of functions that grabs a "Date" and a "Time" from my application and puts the date into as a key, and the time as a multi-dimensional value. 我有一组单独的函数,它们从我的应用程序中获取一个“日期”和一个“时间”,并将日期作为键,将时间作为多维值。

For example purposes: 出于示例目的:

$alldatetimes = array(
    'date1' => array('13:00','14:30','14:30','14:30','15:00'),
    'date2' => array('09:00','10:00','10:30','10:30','12:00')
    );

foreach ($alldatetimes as $date => $times) {
echo '<h1>This Exports:</h1>';  
echo '<h2>'.$date.'</h2><br>';
    foreach ($times as $time) {

        echo $time.'<br>';
    }
}

This exports:
date1
13:00
14:30
14:30
14:30
15:00

date2
09:00
10:00
10:30
10:30
12:00

I'm trying to control if the time is put into the array so only one value each is in the array (I don't want 3 instances of 14:30 for that date). 我试图控制是否将时间放入数组中,以便数组中每个都只有一个值(我不希望该日期有3个实例的14:30)。

Based on other posts here I tried to build something like this to identify if the value was there, but I can't figure out how to tie it all together: 根据此处的其他文章,我尝试构建类似这样的东西来确定值是否存在,但是我不知道如何将它们结合在一起:

function searchForId($id, $array) {
    foreach ($array as $date => $times) {
        foreach ($times as $time) { 
            if ($time === $id) {
                return $time;
            }
        }
    }
    return null;
}

Any ideas? 有任何想法吗?

Update: Here is how the array is initially being created - this probably can be more efficient: 更新:这是最初创建数组的方式-这可能会更有效:

while ($schedule_q -> have_posts() ) : $schedule_q->the_post();
    $alldatetimes [get_the_date()][] = get_the_time();  
endwhile;

You can add a array_unique() call over each sub-array before you loop over your results to ensure it's all unique: 您可以在遍历结果之前,在每个子数组上添加array_unique()调用,以确保其唯一:

foreach ($alldatetimes as &$row) {
    $row = array_unique($row);
}

Output: 输出:

<h1>This Exports:</h1>
<h2>date1</h2><br>
13:00<br>
14:30<br>
15:00<br>
<h1>This Exports:</h1>
<h2>date2</h2><br>
09:00<br>
10:00<br>
10:30<br>
12:00<br>

You can write a recursive function 您可以编写一个递归函数

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}

It is not shown in your question, but how about modifying your function that builds the date/time array to use the times as keys instead of values? 它没有显示在您的问题中,但是如何修改构建日期/时间数组以将时间用作键而不是值的函数? Using something like 使用类似

$alldatetimes[$date][$time]++

in that function would give you an array with one value for each time that would be the number of occurrences of that date/time combination, like this: 该函数将为您提供一个每次都有一个值的数组,该值就是该日期/时间组合的出现次数,如下所示:

$alldatetimes = array(
    'date1' => array('13:00' => 1,'14:30' => 3,'15:00' => 1),
    'date2' => array('09:00' => 1,'10:00' => 1,'10:30' => 2,'12:00' => 1)
    );

Then you could change your code that prints them out to use the key. 然后,您可以更改将其打印出来以使用密钥的代码。

foreach ($times as $time => $count) {
    echo $time.'<br>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM