ash992 Posted May 20, 2014 Share Posted May 20, 2014 Hey guys so I've been trying to figure something out for a day now but I really don't know where to start, basically I'm trying to create some kind of function that goes through my array and checks if the keys value is less than 5, if it is then I want to unset the key removing it from the array, I programmed this underneath as this is basically what I want to achieve however there's a syntax issue as the array_key_excists function doesn't seem to allow the if statement, if anyone knows anything about it it would be very helpful! if (array_key_exists(($key > 5),$data)) { unset($data[$key]); } any help is very much appreciated, Thanks very much in advance! Quote Link to comment https://forums.phpfreaks.com/topic/288625-using-an-if-statement-to-unset-array-keys/ Share on other sites More sharing options...
Ch0cu3r Posted May 20, 2014 Share Posted May 20, 2014 Because you're using array_key_exists wrong, In fact you dont even need to use that function. All you need to do is check the value of $key is less than 5 foreach($data as $key => $val) { if($key < 5) unset($data[$key]); } Quote Link to comment https://forums.phpfreaks.com/topic/288625-using-an-if-statement-to-unset-array-keys/#findComment-1480165 Share on other sites More sharing options...
ash992 Posted May 20, 2014 Author Share Posted May 20, 2014 Hmm there's a few different problems, I'm creating my array from a csv file, after the third comma on every line it has a number that I want to check if it's less than 10, $data = fgetcsv($handle, 12000, "," (this is where I create the variable that reads the csv file) is it possible to pick a specific row in the key to check? or should I try to find a different method of doing this? Thank-you very much for the help and quick response! much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/288625-using-an-if-statement-to-unset-array-keys/#findComment-1480166 Share on other sites More sharing options...
Solution Ch0cu3r Posted May 20, 2014 Solution Share Posted May 20, 2014 So you want to ignore rows where the 4th column has value less than 10? In that case you'd do something like this $data = array(); if (($handle = fopen("data.csv", "r")) !== FALSE) { while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) { // only add rows to the array where the 4th column value is greater than or equal to 10 if($row[3] >= 10) $data[] = $row; } } } Quote Link to comment https://forums.phpfreaks.com/topic/288625-using-an-if-statement-to-unset-array-keys/#findComment-1480168 Share on other sites More sharing options...
ash992 Posted May 20, 2014 Author Share Posted May 20, 2014 Thank-you very much for your time and help Ch0cu3r! hope you have a great day Quote Link to comment https://forums.phpfreaks.com/topic/288625-using-an-if-statement-to-unset-array-keys/#findComment-1480171 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.