|
Posted by michael on 09/06/07 10:31
Hi Jeff,
I don't completely understand what you want to do.
But to search multidimensional arrays for a name or value. Then i
would write a function like this.
//In this example i'll search for values. And i'll return the key name
function searchArray($array,$searchitem){
$keyname ="";
//Loop throw array
foreach($array as $name => $value){
//See if value is a new array
if (is_array($value)){
$keyname=searchArray($value,$searchitem);
if ($keyname != "") return $keyname;
} else {
// the value is not a array but just a value.
if ($value == $searchitem) {
return $name;
}
}
}
return $keyname;
}
// create array
$names = array('Michael'=>'Developer','James'=>'Contact');
// create array
$emails =
array('Michael'=>'michael@server.com','James'=>'james@server.com');
// appends arrays
$people[] = $names;
$people[] = $emails;
//Find my name by email
$person_name = searchArray($people,'michael@server.com');
print "the email michael@server.com is owned by $person_name" ;
On 31 Aug., 23:46, "Jeff" <it_consulta...@hotmail.com.NOSPAM> wrote:
> php 5.x
>
> In my php I define an multidimensional array like this:
> $test = array(array(1, 0 , 3), array('aa', 'dd', 'cc'));
>
> I need to seach the first array ( array(1, 0 , 3) ) of $test for a value...
>
> The first array ( array(1, 0 , 3) ) get it's values from using mt_rand to
> generate random values. I want these integer values to be unique... so I
> need to perform an if-test on the genereated number - if number already
> exist then generate another number and check that one too, until a unique
> number is generated
>
> I'm not sure how to do it so please if you have any suggestions on how to it
> then please post your suggestion here
>
> Jeff
[Back to original message]
|