Jump to content

[SOLVED] Searching For Values On An Array.


Vermillion

Recommended Posts

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?

Link to comment
Share on other sites

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]
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.