Jump to content


Photo

Searching a mulitdimensional array???


  • Please log in to reply
3 replies to this topic

#1 eng_coo

eng_coo
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 17 September 2006 - 01:42 PM

Hi,

I search single dimensional arrays using in_array() function. However i have got a multidimensional array containing users. At the moment i am looping through each array to see if the person exists but as more users get added the code would be very large. Is there an easier way to search a multidimensional array.

Here is my code the array has just 2 users in it at moment -

$people = array (array ("idx"=>"1", UserName=>"John", Password=>"me", FirstName =>"John"),
	          array ("idx"=>"2", UserName =>"Pete", Password=>"pete", FirstName =>"Pete"));

print_r($people);
$user ="John";

if (in_array($user,$people[0])) {
echo "john exists in array 1";
} else {
if (in_array($user,$people[1])) {
echo "john exists in array 2";
} else {
echo "program not worked!!"; }}
echo "<br><br>";

As you can see it searchs each of the internal arrays (0,1). So I i had 60 users it would need 60 loops. There must be a more effective way of searching through an entire mulitdimensional array in one loop as in_array($user, $people) doesnt work as $people shows up as 0=>Array 1=>Array


Any help much appreciated

cheers

John


#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 17 September 2006 - 06:32 PM

Well i've never played with multidimentional arrays before but this seems to work from how far i've tested it..


<?php
$people = array (array ("idx"=>"1", UserName=>"John", Password=>"me", FirstName =>"John"),
	         array ("idx"=>"2", UserName =>"Pete", Password=>"pete", FirstName =>"Pete"));
$user ="John";
$arrayno=0;
foreach ($people as $person) {
$arrayno++;
 if (in_array($user,$person)) {
  echo "$user exists in array $arrayno";
 }
}
?>


Regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 17 September 2006 - 07:06 PM

try
<?php
function array_multi_search(&$haystack, $needle, $key) {
    foreach ($haystack as $k => $data) {
        if ($needle == $data[$key]) return $k;
    }
    return false;
}

$people = array (
        array ("idx"=>"1", UserName=>"John", Password=>"me", FirstName =>"John"),
	    array ("idx"=>"2", UserName =>"Pete", Password=>"pete", FirstName =>"Pete")
        );

$found = array_multi_search($people, 'Pete', 'FirstName');

echo $found;
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 eng_coo

eng_coo
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 19 September 2006 - 12:56 AM

Cheers both of you, both ways worked a charm

cheers

john




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users