gearsgod Posted July 8, 2015 Share Posted July 8, 2015 I keep getting these errors, running php 5.4; Notice: Undefined variable: value in C:\xampp\htdocs\BK\functions.php on line 133Warning: Creating default object from empty value in C:\xampp\htdocs\BK\functions.php on line 133Fatal error: Cannot access empty property in C:\xampp\htdocs\BK\functions.php on line 133 and heres my code; 128 function array_find($parents, $searched) { 129 if (empty($searched) || empty($parents)) { 130 return false; 131 } 132 133 foreach ($parents as $key -> $value) { 134 $exists = true; 135 foreach ($searched as $skey -> $svalue) { 136 $exists = ($exists && IsSet($parents[$key][$skey]) && $parents[$key][$skey] == $svalue); 137 } 138 if($exists){ return $key; } 139 } 140 141 return false; 142 } Quote Link to comment https://forums.phpfreaks.com/topic/297232-small-array-search-just-fails/ Share on other sites More sharing options...
rwhite35 Posted July 8, 2015 Share Posted July 8, 2015 Unless you are in the context of a class with an object, I think you're looking for this: foreach ($parents as $key=>$value) { // -> is for class pointers as in $this->object Quote Link to comment https://forums.phpfreaks.com/topic/297232-small-array-search-just-fails/#findComment-1515926 Share on other sites More sharing options...
gearsgod Posted July 8, 2015 Author Share Posted July 8, 2015 Unless you are in the context of a class with an object, I think you're looking for this: foreach ($parents as $key=>$value) { // -> is for class pointers as in $this->object The errors are gone, but now it does not find conflicts in an array and point out the key for it :/ example: Array ( [0] => Array ( [0] => 12 [1] => 12 [2] => 1pm ) [1] => Array ( [0] => 13 [1] => 13 [2] => 1pm ) ) as you can see, they both are set at 1pm, it needs to return a true, but my script thinks its still false Quote Link to comment https://forums.phpfreaks.com/topic/297232-small-array-search-just-fails/#findComment-1515929 Share on other sites More sharing options...
scootstah Posted July 9, 2015 Share Posted July 9, 2015 foreach ($searched as $skey -> $svalue) { $exists = ($exists && IsSet($parents[$key][$skey]) && $parents[$key][$skey] == $svalue); } if($exists){ return $key; } $exists is only ever going to be equal to the last iteration. So if your last iteration is true, it'll be true - otherwise if the last iteration is false, it'll be false. This will effectively ignore every other iteration. I'm not really sure what your logic is trying to do here, but I think you need to break from the foreach loop. Quote Link to comment https://forums.phpfreaks.com/topic/297232-small-array-search-just-fails/#findComment-1515931 Share on other sites More sharing options...
rwhite35 Posted July 9, 2015 Share Posted July 9, 2015 Are $parents and $searched two different arrays? I can't tell from your logic whether these are two different arrays or different dimension of a multi dim array. The multi dim array above has two dimensions. Is that your $parent (first dimension) and $searched (second dimension). If so, your outer and inner loops maybe confusing the logic. Maybe try a for loop on the inner to help keep things simpler and more manageable. Quote Link to comment https://forums.phpfreaks.com/topic/297232-small-array-search-just-fails/#findComment-1515932 Share on other sites More sharing options...
CroNiX Posted July 9, 2015 Share Posted July 9, 2015 You probably want the return WITHIN your foreach() loop if it finds the value foreach ($searched as $skey => $svalue) { if(isset($parents[$key][$skey]) && $parents[$key][$skey] == $svalue) { return $key; //return the key we found } } return false; //didn't find key also, you probably want "isset()", not "IsSet()" Quote Link to comment https://forums.phpfreaks.com/topic/297232-small-array-search-just-fails/#findComment-1515990 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.