|
|
|
|
|
|
| Author |
Message |
cdf *nix forums Guru
Joined: 25 Apr 2005
Posts: 303
|
Posted: Thu Jul 20, 2006 12:01 pm Post subject:
Array and Search function
|
|
|
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
|
Posted: Thu Jul 20, 2006 12:18 pm Post subject:
Re: Array and Search function
|
|
|
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
|
Posted: Thu Jul 20, 2006 2:26 pm Post subject:
Re: Array and Search function
|
|
|
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 |
|
 |
|
|
The time now is Thu Dec 04, 2008 3:10 am | All times are GMT
|
|
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
|
|