Web Hosting Talk







View Full Version : Search an array for a number?


P-nut
12-16-2006, 07:25 AM
Arrgh..can't get my head around this.

Let's say I have an array that looks something like this:

$var = array('bread', 'pasta', '7', 'pepsi');

And I want to search that array for a number and return it. What would be the easiest way to do so? The number of values in the array would likely change, so using something like $var[2] would not be an option for me.

Any help is much appreciated :D

foobic
12-16-2006, 09:10 AM
I'd start with array_filter($var,'is_numeric') and then maybe array_values

Burhan
12-16-2006, 02:03 PM
<?php

$x = range('a','x');
$find = 'q';
$result = array_search($find,$x);
if ($result !== FALSE)
{
echo 'Found '.$find.' in array at position '.$result."!\n";
} else {
echo 'Did not find '.$find."\n";
}
?>


Result:


Found q in array at position 16!

SpeedEXEC
12-16-2006, 04:07 PM
<?php

$x = range('a','x');
$find = 'q';
$result = array_search($find,$x);
if ($result !== FALSE)
{
echo 'Found '.$find.' in array at position '.$result."!\n";
} else {
echo 'Did not find '.$find."\n";
}
?>


Result:


Found q in array at position 16!



Before seeing his response I would have said:

for($x = 0; $x < count($var); $x++)
{

if($var[$x] == '4')
{
echo "Var num $x has 4!";
}

}

but his way is actually very efficient. But this is just an alternative for ya (not like it's needed, though) :) Good luck!

azizny
12-16-2006, 04:56 PM
foreach($var as $item){
if(is_numeric($item)){ break;}
}
//Item contains the number

foobic
12-16-2006, 07:56 PM
$var = array('bread', 'pasta', '7', 'pepsi');
if ($nums = array_filter($var,'is_numeric')) {
foreach ($nums as $key => $value) print "Found $value at position $key";
} else {
print "No numbers found";
}

mwatkins
12-16-2006, 10:05 PM
Not that this will help your query, but to continue demonstrating the difference between PHP and Python...

Collect a list of numeric values from another list, and turn them into actual integers - this demonstrates an often-used construct in Python called "list comprehensions".

->> var = ['bread', 'pasta', '7', 'pepsi', '123']
->> nums = [int(v) for v in var if v.isdigit()]
->> nums
[7, 123]

Above was simple for clarity; I'd rewrite that slightly for safety:

->> var = ['bread', 'pasta', '7', 'pepsi', '123']
->> nums = [int(v) for v in var if str(v).isdigit()]
->> nums
[7, 123]

To return only the first number found in the list, I'd write a little funtion:

def first_num(list):
for v in list:
if v.isdigit():
return int(v)
->> first_num(var)
7

If I needed to process each number found in the list (PHP 'array') one at a time, but had no control over where/when the iteration would happen, with a minor change to the above function I get a generator (I've renamed it for clarity):

# simply change the return to "yield"
def only_nums(list):
for v in list:
if v.isdigit():
yield int(v)

# using it is slightly different
->> nums = only_nums(var)
->> nums.next()
7
->> nums.next()
123

Burhan
12-17-2006, 01:29 AM
this demonstrates an often-used construct in Python called "list comprehensions"

Let me tell you from personal experience, list comprehensions are one of the most time saving features in Python. Once you use them, you'll wish PHP had them (like I do) :(

Thanks mwatkins for the Python equivalent :)