简体   繁体   English

php中的in_array函数在foreach循环中不起作用

[英]in_array function in php not working in foreach loop

//codeigniter array passed form controller to view    
$data['combined_array'] = array('75x35' => $output,'20x5' => $output1);

$all_dates = Array ('2019-11-19','2019-11-20', '2019-11-21','2019-11-22' );

foreach($combined_array as $key => $value){
    foreach($value as $key1 => $value1)
    {
        foreach($value1 as $key2 => $value2)
        {
            if($value2['size'] == '20x5'){
                if(in_array($value2['date'], $all_dates))
                {
                  echo "exists -".$value2['date'];
                }
                else
                {
                  echo "doesnt exists -".$value2['date'];
                }
            }
        }
    }
}

$value2['date'] contains the values 2019-11-20, 2019-11-21,2019-11-22 . $value2['date']包含值2019-11-20, 2019-11-21,2019-11-22 i want to check if all the values in all_dates exists in $value2['date'] .我想检查 all_dates 中的所有值是否都存在于$value2['date'] if not then echo doesn't exists.如果不是,则 echo 不存在。 but my code doesn't work.但我的代码不起作用。

var_export($combined_array)
array ( '75x35' => array ( '2019-11-19' => array ( 0 => array ( 'size' => '75x35', 'category' => 'steel', 'type' => 'structural', 'sub_type' => 'flats', 'date' => '2019-11-19', 'stock_out' => '4', ), ), '2019-11-20' => array ( 0 => array ( 'size' => '75x35', 'category' => 'steel', 'type' => 'structural', 'sub_type' => 'flats', 'date' => '2019-11-20', 'stock_out' => '6', ), ), '2019-11-21' => array ( 0 => array ( 'size' => '75x35', 'category' => 'steel', 'type' => 'structural', 'sub_type' => 'flats', 'date' => '2019-11-21', 'stock_out' => '5', ), ), '2019-11-22' => array ( 0 => array ( 'size' => '75x35', 'category' => 'steel', 'type' => 'structural', 'sub_type' => 'flats', 'date' => '2019-11-22', 'stock_out' => '5', ), ), ), '20x5' => array ( '2019-11-20' => array ( 0 => array ( 'size' => '20x5', 'category' => 'steel', 'type' => 'structural', 'sub_type' => 'flats', 'date' => '2019-11-20', 'stock_out' => '2', ), ), '2019-11-21' => array ( 0 => array ( 'size' => '20x5', 'category' => 'steel', 'type' => 'structural', 'sub_type' => 'flats', 'date' => '2019-11-21', 'stock_out' => '1', ), ), '2019-11-22' => array ( 0 => array ( 'size' => '20x5', 'category' => 'steel', 'type' => 'structural', 'sub_type' => 'flats', 'date' => '2019-11-22', 'stock_out' => '3', ), ), ), )

I think you want like below: 我想你想像下面这样:

foreach($combined_array as $key => $value){

    $dates = array_unique(array_keys($value));

    if(count(array_diff($all_dates,$dates)) ==0){
        echo "all dates exists on index-".$key.PHP_EOL;
    }else{
        echo "all dates not exists on index-".$key.PHP_EOL;
    }
}

Output:- https://3v4l.org/9giAP 输出: -https : //3v4l.org/9giAP

According to your comment change code a bit:- 根据您的评论更改代码:

foreach($combined_array as $key => $value){

    $dates = array_unique(array_keys($value));
    $arrayDiff = array_diff($all_dates, $dates);

    if(count($arrayDiff) ==0){
        echo "all dates exists on index-".$key.PHP_EOL;
    }else{
        echo "below dates not exists on index-".$key.PHP_EOL;
        print_r($arrayDiff);
    }
}

Output:- https://3v4l.org/BjNQF 输出: -https : //3v4l.org/BjNQF

For checking each date value of each size row replace your if statement with a new one:检查每个size行的每个date值,请用新的if语句替换:

$temp = [];
foreach($combined_array as $key => $value){
    foreach($value as $key1 => $value1)
    {

        foreach($value1 as $key2 => $value2)
        {

            //if($value2['size'] == '20x5'){
            if (!in_array($value2['date'],$temp)){

                $temp[] = $value2['date'];

                if(in_array($value2['date'], $all_dates))
                {  
                  echo "exists -".$value2['date'];
                  echo "\r\n";
                }
                else
                { 
                  echo "doesnt exists -".$value2['date'];
                  echo "\r\n";
                }
            }
        }
    }
}

EDITED: Or you can check all_dates values on existing inside defined size :编辑:或者您可以检查现有内部定义size all_dates值:

$temp = [];

foreach($combined_array as $key => $value){

    if($key == '20x5') { 

        $temp = array_keys($value); 

        $diff = array_diff($all_dates, $temp);

        foreach($all_dates as $date)
        { 
            echo !in_array($date, $diff) ? "exists -".$date.PHP_EOL : "doesnt exists -".$date.PHP_EOL;  
        }
    }
}

Demo演示

A simple solution is for each part of the combined array is to check the difference between the list of dates ( the keys ) and the list of dates you want.对于组合数组的每个部分,一个简单的解决方案是检查日期列表(键)和您想要的日期列表之间的差异。 This can be done using array_keys() to get a list of the dates and array_diff() to check for missing dates...这可以通过使用array_keys()获取日期列表和使用array_diff()检查丢失的日期来完成...

foreach( $combined_array as $key => $value){
    $dateMismatch = array_diff($all_dates, array_keys($value));
    if(count($dateMismatch) > 0 ){
        echo "Dates for {$key} missing=".implode(",", $dateMismatch).PHP_EOL;
    }
}

This just displays the differences as...这只是将差异显示为...

Dates for 20x5 missing=2019-11-19

for your test data.为您的测试数据。

EDIT:编辑:

If you just want the dates different for 1 size, then you don't need a loop, just pick out the details first using something like $combined_array['20x5'] ...如果您只想要 1 个大小的日期不同,那么您不需要循环,只需先使用$combined_array['20x5']类的东西挑选出细节...

$dateMismatch = array_diff($all_dates, array_keys($combined_array['20x5']));
if(count($dateMismatch) > 0 ){
    echo "Dates missing=".implode(",", $dateMismatch).PHP_EOL;
}

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

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