Search the Community
Showing results for tags 'array_search'.
-
Hey everyone!!! It has been years since I have had to opportunity to post on here. I am, however, getting back into development. I am running into an issue and I was looking for some help. Here is the code: $years = array(); //Build the years array, we can only go forward 3 years, backwards 1 year. $sYear = date(Y, strtotime('-1 years')); //Start Year $eYear = date(Y, strtotime('+3 years')); //End Year while($sYear < $eYear){ array_push($years, $sYear); //Add each year that fits to the array $sYear++; } //In order for us to make sure we have clean parameters for our jQuery calls later //We have to make sure that our year variable usage is consistant throughout. //If our case, we are passing the full 4 digit year as am INT = $curYear $curYear = clean($_GET['curYear']); print "Before anything: curYear = $curYear <br><br><br>"; if($curYear == "" || $curYear == null){ //No Year set, use current print "empty or null<br>"; exit; $curYear = date(Y); }elseif(!is_numeric($curYear)){ //Not even a number lol print "not number<br>"; $curYear = date(Y); }elseif($aSearch = in_array($curYear, $years, true)){ print "valid year<br>" . $years[$aSearch]; //It isn't in our array of valid years //$curYear = date(Y); }else{ //all else failed $curYear = date(Y); print "Failed all tests.<br>"; } //Should be a good year for our calendar by this point. exit; I am passing the variable curYear in the url: index.php?curMonth=July&curYear=2013 For some reason, it outputs: Before anything: curYear = 2013 Failed all tests. printing the $years array gets: Array ( [0] => 2013 [1] => 2014 [2] => 2015 [3] => 2016 ) I think you get the just of what I am looking for here. Thanks for the help!!
-
I have this array and I need to search it for the 'id' for a specific value $id. What i'm trying to do is get the position in the array of the id i'm searching for. I have tried $position = array_search($id, $rows); but I get bool false The contents of $rows (taken from MySQL database) is array( { [0]=> array(1) { ["id"]=> string(2) "24" } [1]=> array(1) { ["id"]=> string(2) "22" } [2]=> array(1) { ["id"]=> string(2) "12" } [3]=> array(1) { ["id"]=> string(2) "11" } [4]=> array(1) { ["id"]=> string(1) "8" } [5]=> array(1) { ["id"]=> string(1) "6" } [6]=> array(1) { ["id"]=> string(1) "5" } [7]=> array(1) { ["id"]=> string(1) "4" } }
- 15 replies
-
Hello, I am trying to unset from array using array_search, it's working, except the first array value "a+" is not working $bloodType_list = ['a+','a-','b+','b-','o+','o-','ab+','ab-']; if($key = array_search('a+',$bloodType_list)){ unset($bloodType_list[$key]); } foreach($bloodType_list as $bloodType_lists){ echo $bloodType_lists."<br>"; }
- 7 replies
-
- php
- array_search
-
(and 1 more)
Tagged with: