Vermillion Posted August 25, 2008 Share Posted August 25, 2008 Suppose I have the following code: <?php $register[0] = 0; $register[1] = 1; $register[2] = 1; $register[3] = 1; $register[4] = 0; $register[5] = 1; $register[6] = 0; $register[7] = 1; ?> What would I need to do if I wanted to return, say, ONLY the ones that have "1" with their key values? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted August 25, 2008 Share Posted August 25, 2008 this sounds good for you http://us2.php.net/array_search Quote Link to comment Share on other sites More sharing options...
Vermillion Posted August 25, 2008 Author Share Posted August 25, 2008 With that will I be able to return ALL of them with their respective values though, or only the first match? So I can get something like this: <?php $register[0] = 0; $register[1] = 1; $register[2] = 1; $register[3] = 1; $register[4] = 0; $register[5] = 1; $register[6] = 0; $register[7] = 1; //After using array search, I should only have $register[1], $register[2], $register[3], $register[5], and $register[7] ?> Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted August 25, 2008 Share Posted August 25, 2008 taking 10 seconds to read the page it says you should try http://us2.php.net/manual/en/function.array-keys.php with the optional search parameter set to satisfy the query Quote Link to comment Share on other sites More sharing options...
inactive Posted August 25, 2008 Share Posted August 25, 2008 $register_one = array(); foreach ($register as $key => $value) { if ($value == 1) { $register[$key] = $value; } } $register_one will contain only keys where $value is 1. Quote Link to comment Share on other sites More sharing options...
inactive Posted August 25, 2008 Share Posted August 25, 2008 Sorry, that should have read: $register_one = array(); foreach ($register as $key => $value) { if ($value == 1) { $register_one[$key] = $value; } } Quote Link to comment Share on other sites More sharing options...
Vermillion Posted August 25, 2008 Author Share Posted August 25, 2008 Okay, thanks for that to both of you, I decided to do Cainmi's method, as it looks easier, and I don't get the other one at all. The only problem with the method I chose is that those values are actually generated. Sometimes, the array will have all the values set to 0, and since this methods creates another array, when all the values are at 0, the new array won't be generated, and I was wondering if there was a way to check wether an array exists or not. I tried doing a quick google search, but all the results talk about checking if an element exists, rather than if the whole array exists. Quote Link to comment Share on other sites More sharing options...
inactive Posted August 25, 2008 Share Posted August 25, 2008 Hmm, I'm not sure exactly what you mean there... could you simplify the situation a bit...which vars get generated, what should happen if its empty or not set etc. BTW, you can use isset($var_name) to see if an variable is set, and is_array($var_name) to see if it is an array or not. Quote Link to comment Share on other sites More sharing options...
Vermillion Posted August 25, 2008 Author Share Posted August 25, 2008 Sure. I have a class that registers the users to my site, and every function returns a value: 1 or 0. If a function returns 1, then there was a problem in the registration. So each element in the array will hold 1 or 0, and each key will represent an error message. <?php include "includes/class.register.php"; //Assuming there is user input data code here. $user -> new member; $register[0] = $user -> checkLength("Verm"); //If the username is 3 characters or long, return 0. If it returns 1, display an error message. Here the value will be 0. $register[1] = $user -> checkPassLength("aasdfadg"); // Same as before. This will return 0. $register[2] = $user -> checkMail("sdsdsd") // THis is invalid, therefore it will return 1. //And so on... ?> So suppose all the arrays contain 0, there will be nothing to process on the foreach loop, and I need to do something like "if array exists, do not register users. Else register the users". Ahhh, apologizes if that is not clear. My native language is not English and it is 2:58 AM here. If you need more details, I let me know and I will try to rephrase it. Quote Link to comment Share on other sites More sharing options...
inactive Posted August 25, 2008 Share Posted August 25, 2008 Sorry for late reply. That's no problem. I'll see what I can do. OK so lets get this strait: With your original array: <?php $register[0] = 0; $register[1] = 1; $register[2] = 1; $register[3] = 1; $register[4] = 0; $register[5] = 1; $register[6] = 0; $register[7] = 1; ?> If we put it through the following script: <?php $register_status = array(); // <<-- note this line ###### foreach ($register as $key => $value) { if ($value == 1) { $register_status[$key] = $value; } } ?> Then we will get $register_status being: <?php $register_status[1] = 1; $register_status[2] = 1; $register_status[3] = 1; $register_status[5] = 1; $register_status[7] = 1; ?> Which means that there were a few issues, and the user should not be registered, correct? That means, if we use: <?php $register[0] = 0; $register[1] = 0; $register[2] = 0; $register[3] = 0; $register[4] = 0; $register[5] = 0; $register[6] = 0; $register[7] = 0; ?> Then there wasn't a problem, and running it through the same code above, should mean that $register_status will be an empty array, but as we have initialized the array in the function (see noted line above), it still exists, but has zero elements in it. We can then do: <?php if (count($register_status)) == 0 { // there were no errors so // all elements in $register equaled zero // so $register_status had no elements in it // (which means the count() for $register_status // will be also zero) // and we can now register user } else { // there was one or more errors, do not register user! } ?> I hope I am on the right track, and this helps. You may want to do a bit more coding around exceptions etc (for example, if $register is not an array, then the foreach loop won't even run, errors will be thrown up etc). Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 25, 2008 Share Posted August 25, 2008 You don't need a foreach loop though. cooldude832 gave you the correct link before and the manual does a good job of explaining clearly on how to use the functions. So you can just use array_keys(). $register[0] = 0; $register[1] = 0; $register[2] = 1; $register[3] = 0; $register[4] = 1; $register[5] = 0; $register[6] = 0; $register[7] = 0; $errors = array_keys($register, 1); //The 1 is there so we search the $register array for 1 only. if(count($errors) > 0){ echo "There was an error!"; }else{ echo "No error!"; } The above would output "There was an error!" because there are a few 1's in the $register array. Quote Link to comment Share on other sites More sharing options...
inactive Posted August 25, 2008 Share Posted August 25, 2008 yes i think i over complicated it all lol... Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted August 25, 2008 Share Posted August 25, 2008 yes i think i over complicated it all lol... yes the array functions although they look like they do a foreach loop to find matches they actually do things a lot more simply and optimized. Never loop through an array if it could be large or you have no use to really loop through Quote Link to comment Share on other sites More sharing options...
Vermillion Posted August 26, 2008 Author Share Posted August 26, 2008 You don't need a foreach loop though. cooldude832 gave you the correct link before and the manual does a good job of explaining clearly on how to use the functions. So you can just use array_keys(). $register[0] = 0; $register[1] = 0; $register[2] = 1; $register[3] = 0; $register[4] = 1; $register[5] = 0; $register[6] = 0; $register[7] = 0; $errors = array_keys($register, 1); //The 1 is there so we search the $register array for 1 only. if(count($errors) > 0){ echo "There was an error!"; }else{ echo "No error!"; } The above would output "There was an error!" because there are a few 1's in the $register array. And If I wanted to quote a different error message for each element that has the value of 1? Sorry, I can't seem to figure much about this out. Quote Link to comment Share on other sites More sharing options...
inactive Posted August 26, 2008 Share Posted August 26, 2008 Make an array of error messages (which match the same order that $register is in): <?php $register_error_messages = array( 1 => 'You have not entered a correct username' 2 => 'Your password was not long enough' 3 => 'You smell bad' // etc etc for each of the register processes ); Then use what projectfear said, but with a loop for the errors: <?php $errors = array_keys($register, 1); // the 1 is there so we search the $register array for 1 only. if(count($errors) > 0){ echo 'There was an error! See below:<br />'; foreach ($register as $key => $value) { if ($value == 1) { echo $register_error_messages[$key].'<br />'; } } } else { echo 'No error!'; } Haven't tested it, but should be OK. Quote Link to comment Share on other sites More sharing options...
Vermillion Posted August 26, 2008 Author Share Posted August 26, 2008 Thanks a lot guys, my register script works more than fine now. Quote Link to comment 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.