TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   searching array (http://www.talkphp.com/absolute-beginners/4213-searching-array.html)

allworknoplay 05-03-2009 03:12 AM

searching array
 
Hey guys/gals,

I am trying to output only the items in the array that have ISBN in it. I tried both array_search() and in_array() functions and each time I got errors for using invalid data types in the second field.

Code:

<?php

$test = array('Wfjheighe','ISBN:454656','ISBN:9090943');


    foreach ($test as $id) {
   
           
      if(array_search('ISBN',$id)) echo $id . "<BR />";
   
    }


?>

AND


<?php

$test = array('Wfjheighe','ISBN:454656','ISBN:9090943');


    foreach ($test as $id) {
   
           
      if(in_array('ISBN',$id)) echo $id . "<BR />";
   
    }


?>

I basically just want ISBN:454656 and ISBN:9090943 to be outputted...

What am I doing wrong?

Enfernikus 05-03-2009 03:32 AM

PHP Code:

<?php

$test 
= array('Wfjheighe','ISBN:454656','ISBN:9090943');
$term '/ISBN:(.*?)/Us';

foreach( 
$test as $id )
{
    if( 
preg_match($term$id$match) )
    {
        echo 
$match[0], '<br />';
    }
}

?>

This'll output what you want, how ever for future references some pointers.

when using echo to output data please the comma instead of the period to concatenate strings it is faster.

when you're processing strings without variables in them use single quotes as PHP doesn't process them.

Unless I'm mistaken standards mandate html be lowercase so <BR /> should <br />

P.S - I am not sure this would be the best method.

Kalle 05-03-2009 05:01 AM

You are using in_array incorrectly in the second sample code; You are iterating over a block of elements and pass the second parameter to in_array() as a string where it would expect an array, further though the first parameter for in_array() does not work as a wildcard, it will see if theres an element with that content in this case ISBN and nothing else, meaning it will always return false here.

What you need to do is to use something like str[i]str():
PHP Code:

...
if(
stristr($id'ISBN') !== false)
{
    ...
}
... 


Salathe 05-03-2009 09:41 AM

If you want to go the regular expression route, there's another function which is much friendlier to arrays: preg_grep.

Example
PHP Code:

$test  = array('Wfjheighe','ISBN:454656','ISBN:9090943');
$isbns preg_grep('/^ISBN:\d+$/Di'$test);
print_r($isbns); 

Output
Code:

Array
(
    [1] => ISBN:454656
    [2] => ISBN:9090943
)


allworknoplay 05-03-2009 02:18 PM

Quote:

Originally Posted by Enfernikus (Post 23587)


This'll output what you want, how ever for future references some pointers.

when using echo to output data please the comma instead of the period to concatenate strings it is faster.

when you're processing strings without variables in them use single quotes as PHP doesn't process them.

Unless I'm mistaken standards mandate html be lowercase so <BR /> should <br />

P.S - I am not sure this would be the best method.

Thanks, I had no idea that you can use commas to concatenate strings!? Of all the operators I've been reading about they have never mentioned comma as a concatenater!!!

I guess I got the HTML mandate backwards, I thought it was all capitals....

Thanks for the code.....


Quote:

Originally Posted by Kalle (Post 23591)
You are using in_array incorrectly in the second sample code; You are iterating over a block of elements and pass the second parameter to in_array() as a string where it would expect an array, further though the first parameter for in_array() does not work as a wildcard, it will see if theres an element with that content in this case ISBN and nothing else, meaning it will always return false here.

What you need to do is to use something like str[i]str():
PHP Code:

...
if(
stristr($id'ISBN') !== false)
{
    ...
}
... 


Ohhh I was close. I did try strstr but didn't mention it because I thought I was way off....never tried stristr though.....

*!*

Quote:

Originally Posted by Salathe (Post 23594)
If you want to go the regular expression route, there's another function which is much friendlier to arrays: preg_grep.

Example
PHP Code:

$test  = array('Wfjheighe','ISBN:454656','ISBN:9090943');
$isbns preg_grep('/^ISBN:\d+$/Di'$test);
print_r($isbns); 

Output
Code:

Array
(
    [1] => ISBN:454656
    [2] => ISBN:9090943
)


I'll take a look at your code as well, I need to get better at Regex....

As always thanks!

sketchMedia 05-03-2009 03:48 PM

Quote:

Thanks, I had no idea that you can use commas to concatenate strings!? Of all the operators I've been reading about they have never mentioned comma as a concatenater!!!
That isn't a concatenater, echo can take multiple parameters, that's what the comma is (like you would a function).


All times are GMT. The time now is 06:28 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0