Jump to content

Recommended Posts

I have an array of about 99 numbers in a list which are comma seperated... and currently im just getting a $var and using a while loop to check through the array to see if its in the array or not.

 

Now with 99 numbers and if the $var was 98 ... it has to while loop 98 times till it finds it, which is going to be a bit of power hogger if im looking through many numbers.

 

Is there any array search function so that itll find the first then second digit of a $var? That would limit it to max of 20 checks because it only needs to search 0 to 9 and then 0 to 9 again.

 

If so what function can do this?

I have an array of about 99 numbers in a list which are comma seperated... and currently im just getting a $var and using a while loop to check through the array to see if its in the array or not.

 

If your ultimate goal is to see if the number is in the array you can just use in_array().

 

Is there any array search function so that itll find the first then second digit of a $var?

 

Not really sure what that means...  You can get the first and second digit of $var by doing:

 

$var = "98";
echo "First Digit: " . $var[0];
echo "
Second Digit: " . $var[1];
?>

 

Hope this helps.

100 numbers wouldn't be worth coming up with an indexing system.

 

Thanks for the replies everyone. And I agree with out Corbin, though in due course the total numbers involved will reach the thousands over time so its better i plan ahead for efficientcy.. so sounds like binary search algorithm is needed i'll do some searching on it!

The only problem with a binary search is that the values need to be ordered.  Will the values be in order already?  If not, it would be less intense to just loop through the array than sort it most likely ;p.  (Sorting for people is easy and quick, but sorting for computers is a bit different.)

<?php

$numbers = range(0, 100);
//Shuffle it so it's not in order ;p
shuffle($numbers);

$orig_numbers = $numbers;

$START = microtime(true);

for($i = 0; $i < 10000; ++$i) {
sort($numbers);
shuffle($numbers);
}

$END = microtime(true);

echo ($END - $START) . PHP_EOL;


$START = microtime(true);

for($i = 0; $i < 10000; ++$i) {
in_array(rand(50, 150), $orig_numbers);
shuffle($numbers);
}

$END = microtime(true);

echo ($END - $START) . PHP_EOL;

 

Output:

 

0.51721501350403

0.13445687294006

 

 

So, it takes about 1/4 the time to just do in_array in that little script.  Note that an extra rand() call is in the in_array block too.

 

But, that is flawed since it doesn't explore how well the set would scale.

 

 

But I would imagine that in_array will always beat sort(), and that's before you even do the binary tree stuff ;p.

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.