in_array not working

Discussion in 'PHP' started by *louie*, Oct 10, 2007.

  1. #1
    I have the following result in an array:
    
    Array ( [0] => Array ( [x_list_id] => 14 [client_id] => 6 [list_id] => 36 [x_list_name] => Chocolate [x_list_option] => choice [x_list_price] => 0 [x_list_status] => active [x_list_added] => 2007-10-09 17:20:35 ) [1] => Array ( [x_list_id] => 17 [client_id] => 6 [list_id] => 36 [x_list_name] => Cookies and Cream [x_list_option] => choice [x_list_price] => 0 [x_list_status] => active [x_list_added] => 2007-10-09 17:21:17 ) [2] => Array ( [x_list_id] => 13 [client_id] => 6 [list_id] => 36 [x_list_name] => Haagen-Dazs [x_list_option] => choice [x_list_price] => 0 [x_list_status] => active [x_list_added] => 2007-10-09 17:20:22 ) [3] => Array ( [x_list_id] => 16 [client_id] => 6 [list_id] => 36 [x_list_name] => Strawberry [x_list_option] => choice [x_list_price] => 0 [x_list_status] => active [x_list_added] => 2007-10-09 17:21:03 ) [4] => Array ( [x_list_id] => 15 [client_id] => 6 [list_id] => 36 [x_list_name] => Vanilla [x_list_option] => choice [x_list_price] => 0 [x_list_status] => active [x_list_added] => 2007-10-09 00:00:00 ) ) 
    PHP:
    </DIV>
    and i am trying to find if some value exits using the in_array function but can get it to run properly:

    if(in_array("choice",$rows)){ $choice = "true";}if(in_array("standard",$rows)){ $standard = "true";}
    if(in_array("extra",$rows)){ $extra = "true";}
    PHP:
    what am I doing wrong here?
     
    *louie*, Oct 10, 2007 IP
  2. mikemotorcade

    mikemotorcade Peon

    Messages:
    645
    Likes Received:
    49
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Are you sure you're building your array correctly? If what you put there is the code you are using, then the variable "$rows" doesn't actually exist. As far as I can tell your in_array code looks alright. That means the error is probably in how the array is being built.
     
    mikemotorcade, Oct 10, 2007 IP
  3. *louie*

    *louie* Peon

    Messages:
    48
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    this is how I bulid the array
    
    if ($nTotalRecsListX > 0) {
    		$rows = array();
    		while($rowListX = phpmkr_fetch_assoc($rslistX)){
    			$rows[] = $rowListX;//build array list
    		}
    		//print_r($rows);
    	}
    
    PHP:
     
    *louie*, Oct 10, 2007 IP
  4. mikemotorcade

    mikemotorcade Peon

    Messages:
    645
    Likes Received:
    49
    Best Answers:
    0
    Trophy Points:
    0
    #4
    And you've echoed the array to make sure that those values definitely are in the array, right?
     
    mikemotorcade, Oct 10, 2007 IP
  5. *louie*

    *louie* Peon

    Messages:
    48
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That's where the first code posted above is coming from (print_r($rows);)
     
    *louie*, Oct 10, 2007 IP
  6. mikemotorcade

    mikemotorcade Peon

    Messages:
    645
    Likes Received:
    49
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hmm... is the $row array being populated within a function? Or is it on the same level as the other code?
     
    mikemotorcade, Oct 10, 2007 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    in_array() does not work recursively. Use this function instead.

    
    function in_array_recursive($needle, $haystack, $strict = false)
    {
    	foreach ($haystack AS $hay)
    	{
    		if (is_array($hay))
    		{
    			return in_array_recursive($needle, $hay, $strict);
    		}
    		else if ((!$strict AND $hay == $needle) OR ($strict AND $hay === $needle))
    		{
    			return true;
    		}
    	}
    	
    	return false;
    }
    
    PHP:
     
    nico_swd, Oct 10, 2007 IP
    mikemotorcade likes this.
  8. mikemotorcade

    mikemotorcade Peon

    Messages:
    645
    Likes Received:
    49
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Aha! I was scratching my head at that one. Good job, nico_swd! Rep for you!
     
    mikemotorcade, Oct 10, 2007 IP
  9. *louie*

    *louie* Peon

    Messages:
    48
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    this is what I did and it works fine.

    I was already looping through the arrays to write certain values but never came to me that i should do another loop to see if in_array.

    anyhow this is what i sis and it works:

    
    foreach($rows as $row){ 
                                    if(in_array("choice",$row)){ $choice = "true";} 
                                    if(in_array("standard",$row)){ $standard = "true";} 
                                    if(in_array("extra",$row)){ $extra = "true";} 
                            } 
    
    PHP:

    Thanks for help.
     
    *louie*, Oct 10, 2007 IP