简体   繁体   English

使用in_array()的Foreach循环不起作用

[英]Foreach loop with in_array() Not Working

I am having trouble getting an in_array() to work. 我无法让in_array()工作。 The in_array() is returning false thus the array_push is not being called. in_array()返回false,因此不调用array_push。 I cannot figure out why. 我无法弄清楚为什么。 The $zipsArray and $propsArray both have data and I have confirmed that there are matching zips in each array. $ zipsArray和$ propsArray都有数据,我已经确认每个数组中都有匹配的拉链。 I have also confirmed that each $val has a value. 我还确认每个$ val都有一个值。

Help is much appreciated! 非常感谢帮助!


        $zipsArray = self::getZipRadius('vhdf',100);

        $propsArray = self::allProps($dbc,$source);

        $localArray = array();

        foreach($propsArray as $key => $val){

            if(in_array($val['zip'], $zipsArray, true)) {

                $propitem = array(
                    'propid'    => $val['propid'],
                    'street'    => $val['street'],
                    'city'      => $val['city'],
                    'state'     => $val['state'],
                    'zip'       => $val['zip'],
                    'beds'      => $val['beds'],
                    'baths'     => $val['baths'],
                    'garage'    => $val['garage'],
                    'type'      => $val['type'],
                    'sqft'      => str_replace(array('.', ','), '' , $val['sqft']),
                    'descript'  => self::clearString($val['descript']),
                    'listdate'  => $val['listdate'],
                    'askprice'  => $val['askprice'],
                    'repairval' => $val['repairval'],
                    'source'    => $val['source']
                );

                array_push($localArray, $propitem);

            }   

        }

        return $localArray;

The $zipsArray currently has this data. $ zipsArray目前有这些数据。 I shortened it a bit to save scrolling as it currently holds 622 items. 我缩短了一点来保存滚动,因为它目前拥有622个项目。

array(1853) { [0]=> string(5) "12723" [1]=> string(5) "12736" [2]=> string(5) "12736" [3]=> string(5) "12741" [4]=> string(5) "12745" [5]=> string(5) "12760" [6]=> string(5) "12766" [7]=> string(5) "12767" [8]=> string(5) "13020" [9]=> string(5) "13021" [10]=> string(5) "13021" [11]=> string(5) "13021" [12]=> string(5) "13021" [13]=> string(5) "13021" [14]=> string(5) "13021" [15]=> string(5) "13022" [16]=> string(5) "13026" [17]=> string(5) "13026" [18]=> string(5) "13027" [19]=> string(5) "13027" [20]=> string(5) "13027" [21]=> string(5) "13027" [22]=> string(5) "13027" [23]=> string(5) "13028" [24]=> string(5) "13029" [25]=> string(5) "13030" [26]=> string(5) "13031" [27]=> string(5) "13031" [28]=> string(5) "13031" [29]=> string(5) "13032" [30]=> string(5) "13032" [31]=> string(5) "13032" [32]=> string(5) "13032" [33]=> string(5) "13033" [34]=> string(5) "13034" [35]=> string(5) "13035" [36]=> string(5) "13035" [37]=> string(5) "13035" [38]=> string(5) "13035" [39]=> string(5) "130 array(1853){[0] => string(5)“12723”[1] => string(5)“12736”[2] => string(5)“12736”[3] => string(5) “12741”[4] => string(5)“12745”[5] => string(5)“12760”[6] => string(5)“12766”[7] => string(5)“12767 “[8] => string(5)”13020“[9] => string(5)”13021“[10] => string(5)”13021“[11] => string(5)”13021“[ 12] => string(5)“13021”[13] => string(5)“13021”[14] => string(5)“13021”[15] => string(5)“13022”[16] => string(5)“13026”[17] => string(5)“13026”[18] => string(5)“13027”[19] => string(5)“13027”[20] => string(5)“13027”[21] => string(5)“13027”[22] => string(5)“13027”[23] => string(5)“13028”[24] => string( 5)“13029”[25] => string(5)“13030”[26] => string(5)“13031”[27] => string(5)“13031”[28] => string(5) “13031”[29] => string(5)“13032”[30] => string(5)“13032”[31] => string(5)“13032”[32] => string(5)“13032 “[33] => string(5)”13033“[34] => string(5)”13034“[35] => string(5)”13035“[36] => string(5)”13035“[ 37] => string(5)“13035”[38] => string(5)“13035”[39] => string(5)“130 36" [40]=> } 36“[40] =>}

An example of $val is $ val的一个例子是

[52]=>
  array(15) {
    ["propid"]=>
    string(6) "274672"
    ["street"]=>
    string(10) "320 Easton"
    ["city"]=>
    string(5) "Depew"
    ["state"]=>
    string(2) "NY"
    ["zip"]=>
    string(5) "14043"
    ["beds"]=>
    string(1) "2"
    ["baths"]=>
    string(1) "1"
    ["garage"]=>
    string(1) "0"
    ["type"]=>
    string(13) "Single Family"
    ["sqft"]=>
    string(3) "972"
    ["descript"]=>
    string(365) "This 972 square foot single family home has 2 bedrooms and 1.0 bathrooms. Located in a desirable part of Depew, property has a lot to offer besides the location in a quite neighborhood it includes a nice size back yard, small front yard for easy maintenance. Just off of a main road close to amenities. 

This Discounted property wont last long contact today."
    ["listdate"]=>
    string(10) "2016-07-13"
    ["askprice"]=>
    string(5) "25000"
    ["repairval"]=>
    string(6) "75,000"
    ["source"]=>
    string(4) "prop"
  }

Debug reults from in_array(): 来自in_array()的调试结果:

string(5) "33511" bool(false) bool(false) int(530) string(5) "33442" bool(false) bool(false) int(530) string(5) "33322" bool(false) bool(false) int(530) string(5) "32258" bool(false) bool(false) int(530) string(5) "32220" bool(false) bool(false) int(530) string(5) "34759" bool(false) bool(false) int(530) string(5) "38106" bool(false) bool(false) int(530) string(5) "32835" bool(false) bool(false) int(530) string(5) "38107" bool(false) bool(false) int(530) string(5) "38107" bool(false) bool(false) int(530) string(5) "38107" bool(false) bool(false) int(530) string(5) "38107" bool(false) bool(false) int(530) string(5) "38128" bool(false) bool(false) int(530) string(5) "64256" bool(false) bool(false) int(530) string(5) "34797" bool(false) bool(false) int(530) string(5) "87108" bool(false) bool(false) int(530) string(5) "32205" bool(false) bool(false) int(530) string(5) "04038" bool(false) bool(false) int(530) string(5) "30273" bool(false) bool(false) int(530) string(5) "32208" bool(false) bool(false) int(530) string(5) "28304" bool(false) bool(false) int(530) string(5) "06710" bool(false) bool(false) int(530) string(5) "46970" bool(false) bool(false) int(530) string(5) "07107" bool(false) bool(false) int(530) string(5) "32922" bool(false) bool(false) int(530) string(5) "32922" bool(false) bool(false) int(530) string(5) "32828" bool(false) bool(false) int(530) string(5) "34748" bool(false) bool(false) int(530) string(5) "32114" bool(false) bool(false) int(530) string(5) "34769" bool(false) bool(false) int(530) string(5) "76111" bool(false) bool(false) int(530) string(5) "32209" bool(false) bool(false) int(530) string(5) "38128" bool(false) bool(false) int(530) string(5) "77091" bool(false) bool(false) int(530) string(5) "32209" bool(false) bool(false) int(530) string(5) "32209" bool(false) bool(false) int(530) string(5) "32209" bool(false) bool(false) int(530) string(5) "32204" bool(false) bool(false) int(530) string(5) "32208" bool(false) bool(false) int(530) string(5) "32208" bool(false) bool(false) int(530) string(5) "32209" bool(false) bool(false) int(530) string(5) "32204" bool(false) bool(false) int(530) string(5) "32217" bool(false) bool(false) int(530) string(5) "32206" bool(false) bool(false) int(530) string(5) "28301" bool(false) bool(false) int(530) string(5) "32736" bool(false) bool(false) int(530) string(5) "91706" bool(false) bool(false) int(530) string(5) "38109" bool(false) bool(false) int(530) string(5) "30315" bool(false) bool(false) int(530) string(5) "30331" bool(false) bool(false) int(530) string(5) "28301" bool(false) bool(false) int(530) string(5) "30318" bool(false) bool(false) int(530) string(5) "14043" bool(false) bool(false) int(530) string(5) "30344" bool(false) bool(false) int(530) string(5) "32168" bool(false) bool(false) int(530) string(5) "33881" bool(false) bool(false) int(530) string(5) "34748" bool(false) bool(false) int(530) string(5) "32137" bool(false) bool(false) int(530) string(5) "73110" bool(false) bool(false) int(530) string(5) "33844" bool(false) bool(false) int(530) string(5) "44118" bool(false) bool(false) int(530) string(5) "60409" bool(false) bool(false) int(530) string(5) "83634" bool(false) bool(false) int(530) string(5) "87107" bool(false) bool(false) int(530) string(5) "32810" bool(false) bool(false) int(530) string(5) "60633" bool(false) bool(false) int(530) string(5) "32216" bool(false) bool(false) int(530) string(5) "32725" bool(false) bool(false) int(530) string(5) "60643" bool(false) bool(false) int(530) string(5) "85123" bool(false) bool(false) int(530) string(5) "51501" bool(false) bool(false) int(530) string(5) "60827" bool(false) bool(false) int(530) string(5) "37404" bool(false) bool(false) int(530) string(5) "19140" bool(false) bool(false) int(530) string(5) "14131" bool(false) bool(false) int(530) string(5) "19082" bool(false) bool(false) int(530) string(5) "60426" bool(false) bool(false) int(530) string(5) "48209" bool(false) bool(false) int(530) string(5) "18337" bool(false) bool(false) int(530) string(5) "60639" bool(false) bool(false) int(530) string(5) "48198" bool(false) bool(false) int(530) string(5) "60827" bool(false) bool(false) int(530) string(5) "49648" bool(false) bool(false) int(530) string(4) "6610" bool(false) bool(false) int(530) string(5) "99999" bool(false) bool(false) int(530) string(5) "47512" bool(false) bool(false) int(530) string(5) "99999" bool(false) bool(false) int(530) string(5) "23607" bool(false) bool(false) int(530) string(5) "99999" bool(false) bool(false) int(530) string(5) "99999" bool(false) bool(false) int(530) string(5) "99999" bool(false) bool(false) int(530) string(5) "99999" bool(false) bool(false) int(530) string(5) "99999" bool(false) bool(false) int(530) string(5) "99999" bool(false) bool(false) int(530) string(5) "99999" bool(false) bool(false) int(530) string(5) "99999" bool(false) bool(false) int(530) string(5) "99999" bool(false) bool(false) int(530) string(5) "99999" bool(false) bool(false) int(530) string(5) "99999" bool(false) bool(false) int(530) string(5) "13205" bool(true) bool(true) int(530) string(5) "32539" bool(false) bool(false) int(530) string(5) "62206" bool(false) bool(false) int(530) string(5) "38107" bool(false) bool(false) int(530) string(5) "44125" bool(false) bool(false) int(530) string(5) "37921" bool(false) bool(false) int(530) string(5) "44120" bool(false) bool(false) int(530) string(5) "40845" bool(false) bool(false) int(530) string(5) "34420" bool(false) bool(false) int(530) string(5) "43725" bool(false) bool(false) int(530) string(5) "44052" bool(false) bool(false) int(530) string(5) "36701" bool(false) bool(false) int(530) string(5) "45504" bool(false) bool(false) int(530) string(5) "35206" bool(false) bool(false) int(530) string(5) "48205" bool(false) bool(false) int(530) string(5) "48458" bool(false) bool(false) int(530) string(5) "64492" bool(false) bool(false) int(530) string(5) "99999" bool(false) bool(false) int(530) string(5) "60653" bool(false) bool(false) int(530) string(5) "64130" bool(false) bool(false) int(530) string(5) "26003" bool(false) bool(false) int(530) string(5) "47807" bool(false) bool(false) int(530) string(5) "39212" bool(false) bool(false) int(530) string(5) "63118" bool(false) bool(false) int(530) string(5) "20621" bool(false) bool(false) int(530) string(5) "77830" bool(false) bool(false) int(530) string(5) "51652" bool(false) bool(false) int(530) string(5) "27530" bool(false) bool(false) int(530) string(5) "48089" bool(false) bool(false) int(530) string(5) "60651" bool(false) bool(false) int(530) string(5) "63135" bool(false) bool(false) int(530)

If the third parameter strict of in_array is set to TRUE then the in_array() function will also check the types of the needle in the haystack. 如果in_array的第三个参数strict设置为TRUE,那么in_array()函数也将检查haystack中针的类型。

Remove the strict mode (third_parameter) or make sure both arrays contains zip codes as string (The second is more fool proof). 删除严格模式 (third_parameter) 或确保两个数组都包含zip代码作为字符串 (第二个是更简单的证明)。 Debug data shows one of the arrays containing zip codes as a string while the other contains it as an integer. 调试数据显示其中一个包含邮政编码的数组作为字符串,而另一个数组包含整数。

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

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