Jump to content

[SOLVED] Simple in_array help


rastlin

Recommended Posts

This has more to do with syntax then anything so I'm hoping it is not in the wrong spot.

 

This is my code, I think I'm thinking is right but doing it the hard way....

 

if (in_array($check_array[$i],$sess_three)) {
		  //The value is in the array
		  
	  }else{
		  //Value is not in array so add it and store array into session
		  
		  //something in array add it to the session array
              $sess_three[] .= $check_array[$i];
	  }//End of If in-array test

 

 

Ok I was wondering if I could change that to this and have the same result

 

		  if (!in_array($check_array[$i],$sess_three)) {
		  //something in array add it to the session array
              $sess_three[] .= $check_array[$i];
	  }//End of If in-array test

 

 

Hope I'm not bothering ya'll to much, hope to hear from you soon and many thanks

 

Stephen

Link to comment
https://forums.phpfreaks.com/topic/131215-solved-simple-in_array-help/
Share on other sites

yes...if you don't want to do anything if it's already in the array, then your approach is correct. one thing i will point out though, is there shouldn't  be a . before the =

 

        if (!in_array($check_array[$i],$sess_three)) {
           //something in array add it to the session array
              $sess_three[] = $check_array[$i];
        }//End of If in-array test

Ok but would that still add it the the array with the values that are in it?

 

$sess_three[] = $check_array[$i];

 

I always thought you had to add the . to keep the current content of the array while adding the new value on the end, if I'm wrong please let me know....

 

 

many thanks

 

Stephen

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.