473,480 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Array and Search function

Gi
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
Jul 20 '06 #1
2 1372
Gi a écrit :
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
Jul 20 '06 #2
Gi wrote:
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));

Jul 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2855
by: Nico | last post by:
Hello folks, I am currently storing a set of objects inside an array, $itemlist = array(); $itemlist = new item("myitem"); //... and I am looking to develop a search function, which...
5
2919
by: Andrew Poulos | last post by:
If I'm searching for an occurance of a value in a multi-dimensional array how can I get it's index returned as an array, if found? For example, if: foo = new Array(); foo = , 5, , 9, 10]; ...
1
2330
by: aemazing | last post by:
hello, im new to the forum and i wanted to help with my c++ program. the teacher wants us to design a progrm that would keep track of airplanes awaitin landing at an airport. the program will...
1
2184
by: aemazing | last post by:
i've been tryin to do the following - -Add a new flight number to the end of the queue (got it done) -LAnd the plane at the front of the queue - problems wit it- -display the queue - got it done...
11
39423
by: deko | last post by:
I need to create a basic one-dimensional array of strings, but I don't know how many strings I'm going to have until the code is finished looping. pseudo code: Dim astrMyArray() Do While Not...
3
1833
by: Carramba | last post by:
hi! the code is cinpiling with gcc -ansi -pedantic. so Iam back to my question Iam trying to make program were I enter string and serach char. and funktion prints out witch position char is...
23
7363
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
7
11761
by: ianenis.tiryaki | last post by:
well i got this assignment which i dont even have a clue what i am supposed to do. it is about reading me data from the file and load them into a parallel array here is the question: Step (1) ...
9
1489
by: Peri | last post by:
Dear All, I have written a common array class which has insert, search and other functions. For Example (Code in VB.NET): ' Client Class Public Class Client Public ClientCode As String ...
3
1422
by: alexmason86 | last post by:
getting a bit stuck here got some code that allows a user to input numbers to a 3x3 array. but next on the tutorial sheet i have to then ask the user for a number to search for and then search the...
0
7054
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6918
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7102
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5357
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4495
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3008
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1310
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.