PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > search in an array of array
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
search in an array of array

Réponse
 
LinkBack Outils de la discussion
Vieux 05/11/2007, 14h44   #1
Bob Bedford
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut search in an array of array

Hello,

I've an array of array(1,2,3,4). I'd like to retrieve the values in 3 and 4
giving the values 1 and 2. How can I do that, how to search in an array of
array ? in short, how to write the findvalue function ?

$list = array();
array_push($list,array(1,1,'x','x');
array_push($list,array(1,2,'x','y');
array_push($list,array(2,1,'y','x');
array_push($list,array(2,2,'y','y');

$result = array();
$result = findvalue(1,2); //should return 'x','y'

function findvalue($a,$b){
//return the array of values...
}

Thanks for ing.

Bob


  Réponse avec citation
Vieux 05/11/2007, 16h14   #2
Steve
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: search in an array of array

> $list = array();
> array_push($list,array(1,1,'x','x');
> array_push($list,array(1,2,'x','y');
> array_push($list,array(2,1,'y','x');
> array_push($list,array(2,2,'y','y');
>
> $result = array();
> $result = findvalue(1,2); //should return 'x','y'
>
> function findvalue($a,$b){
> //return the array of values...
> }


print_r($list[1][2]);


not sure why you need a function here.


  Réponse avec citation
Vieux 05/11/2007, 16h19   #3
Darko
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: search in an array of array

On Nov 5, 3:44 pm, "Bob Bedford" <b...@bedford.com> wrote:
> Hello,
>
> I've an array of array(1,2,3,4). I'd like to retrieve the values in 3 and 4
> giving the values 1 and 2. How can I do that, how to search in an array of
> array ? in short, how to write the findvalue function ?
>
> $list = array();
> array_push($list,array(1,1,'x','x');
> array_push($list,array(1,2,'x','y');
> array_push($list,array(2,1,'y','x');
> array_push($list,array(2,2,'y','y');
>
> $result = array();
> $result = findvalue(1,2); //should return 'x','y'
>
> function findvalue($a,$b){
> //return the array of values...
>
> }
>
> Thanks for ing.
>
> Bob


What have you tried already?
We won't do your homeworks ;-)

  Réponse avec citation
Vieux 05/11/2007, 16h38   #4
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: search in an array of array

On Mon, 05 Nov 2007 17:14:20 +0100, Steve <no.one@example.com> wrote:

>> $list = array();
>> array_push($list,array(1,1,'x','x');
>> array_push($list,array(1,2,'x','y');
>> array_push($list,array(2,1,'y','x');
>> array_push($list,array(2,2,'y','y');
>>
>> $result = array();
>> $result = findvalue(1,2); //should return 'x','y'
>>
>> function findvalue($a,$b){
>> //return the array of values...
>> }

>
> print_r($list[1][2]);


Yet he wants to find the record in $list[1].

OP: for a single search a foreach loop or customized array_filter come to
mind. If you have to search a lot of entries, you might want to build an
array of references, indezing the array. Be very, very sure you need it
though, your code becomes somewhat unreadable by another coder.
--
Rik Wasmus
  Réponse avec citation
Vieux 06/11/2007, 11h20   #5
Bob Bedford
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: search in an array of array


"Darko" <darko.maksimovic@gmail.com> a écrit dans le message de news:
1194279540.701715.22630@22g2000hsm.googlegroups.co m...
> On Nov 5, 3:44 pm, "Bob Bedford" <b...@bedford.com> wrote:
>> Hello,
>>
>> I've an array of array(1,2,3,4). I'd like to retrieve the values in 3 and
>> 4
>> giving the values 1 and 2. How can I do that, how to search in an array
>> of
>> array ? in short, how to write the findvalue function ?
>>
>> $list = array();
>> array_push($list,array(1,1,'x','x');
>> array_push($list,array(1,2,'x','y');
>> array_push($list,array(2,1,'y','x');
>> array_push($list,array(2,2,'y','y');
>>
>> $result = array();
>> $result = findvalue(1,2); //should return 'x','y'
>>
>> function findvalue($a,$b){
>> //return the array of values...
>>
>> }
>>
>> Thanks for ing.
>>
>> Bob

>
> What have you tried already?
> We won't do your homeworks ;-)
>
>

I've a loop that goes trough all records of $list and check the values...not
sure is the best way to do so...my $list array contains about 1500 records
each is an array of 5 fields.

function findvalue($check1,$check2){
global $list

foreach ($list as $key => $value){
if(($value[0] = = $check1) and ($value[1] = = $check2)){
return array(strtoupper($value[3]),strtoupper($value[2]));
break;
}
}
}


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h27.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13290 seconds with 13 queries