niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » PHP
Array and Search function
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
Author Message
cdf
*nix forums Guru


Joined: 25 Apr 2005
Posts: 303

PostPosted: Thu Jul 20, 2006 12:01 pm    Post subject: Array and Search function Reply with quote

Hello,

I'm a new Php user.
I'm trying to use the array functions and would like to ask a suggestion, if
possible.
I wrote the following sample code:

<?php
$test[1]="tizio";
$test[2]="caio";
$test[3]="caio";
print(array_search("caio", $test));
?>

It's a simple array with a command which searches the corresponding key for
the string "caio".
As result I've 2 and not, as I thought 2, 3. Do you know is it possible to
search all the keys associated to the string "caio"? I tried also the
array_keys function but in this case the result has too much information.
I'd like to have as result only 2 and 3.
Pleace accept my apologies for the question and for the trouble.
Really many thanks for the possible suggestions.

Best,
Gi
Back to top
Laurent Duretz
*nix forums beginner


Joined: 20 Jul 2006
Posts: 2

PostPosted: Thu Jul 20, 2006 12:18 pm    Post subject: Re: Array and Search function Reply with quote

Gi a écrit :
Quote:
Hello,

I'm a new Php user.
I'm trying to use the array functions and would like to ask a suggestion, if
possible.
I wrote the following sample code:

?php
$test[1]="tizio";
$test[2]="caio";
$test[3]="caio";
print(array_search("caio", $test));
?

It's a simple array with a command which searches the corresponding key for
the string "caio".
As result I've 2 and not, as I thought 2, 3. Do you know is it possible to
search all the keys associated to the string "caio"? I tried also the
array_keys function but in this case the result has too much information.
I'd like to have as result only 2 and 3.
Pleace accept my apologies for the question and for the trouble.
Really many thanks for the possible suggestions.

Best,
Gi



Hi,

You should use array_keys()

Laurent Duretz
Back to top
chernyshevsky@hotmail.com
*nix forums Guru


Joined: 09 Mar 2005
Posts: 871

PostPosted: Thu Jul 20, 2006 2:26 pm    Post subject: Re: Array and Search function Reply with quote

Gi wrote:
Quote:
Hello,

I'm a new Php user.
I'm trying to use the array functions and would like to ask a suggestion, if
possible.
I wrote the following sample code:

?php
$test[1]="tizio";
$test[2]="caio";
$test[3]="caio";
print(array_search("caio", $test));
?

It's a simple array with a command which searches the corresponding key for
the string "caio".
As result I've 2 and not, as I thought 2, 3. Do you know is it possible to
search all the keys associated to the string "caio"? I tried also the
array_keys function but in this case the result has too much information.
I'd like to have as result only 2 and 3.
Pleace accept my apologies for the question and for the trouble.
Really many thanks for the possible suggestions.

Best,
Gi

The function you need is array_intersect(). Example:

$test[1]="tizio";
$test[2]="caio";
$test[3]="caio";

$search = array("caio");
var_dump(array_intersect($test, $search));
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
The time now is Thu Dec 04, 2008 3:10 am | All times are GMT
navigation Forum index » Programming » PHP
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Best method to search kristy420 Oracle 0 Tue Nov 18, 2008 6:34 pm
No new posts Function Pointer Sikandar C 3 Fri Jul 21, 2006 1:23 pm
No new posts Bug#379104: ITP: complearn-mpi -- parallel quartet tree s... Rudi Cilibrasi devel 0 Fri Jul 21, 2006 11:30 am
No new posts Arbitrary function with parameter darknails@gmail.com C++ 2 Fri Jul 21, 2006 9:58 am
No new posts IMAP4 search with special characters Luis Corrales python 0 Fri Jul 21, 2006 7:51 am

Credit Cards | Hosting | Mortgage Calculator | Internet Advertising | Mortgage Loans
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1975s ][ Queries: 16 (0.1179s) ][ GZIP on - Debug on ]