This is a discussion on finding value in multidimensional array within the alt.comp.lang.php forums, part of the PHP Programming Forums category; php 5.x In my php I define an multidimensional array like this: $test = array(array(1, 0 , 3), array('...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
Jeff 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 > You are not very clear about what you want to achieve. If what you want is an array filled with unique numbers from 0 to N, in random order, then you can do it in two steps. 1) Fill the array with 0 to N in any order (a[i] = i is just fine). 2) Swap the ith element with a random element for i = 0 to N. <?php $s = 5; $a = array(); for ($i=0; $i<$s; $i++) $a[$i] = $i; for($i = 0; $i<$s; $i++) { $j = mt_rand(0,$s-1); $t = $a[$j]; $a[$j] = $a[$i]; $a[$i] = $t; } print_r($a); ?> Regards Ian |
|
|||
|
Just a thought: you might want to try doing array_search() against
$outerArray[0] - as that will search the outer array's first array - which I believe is your unique IDs. If your second array is values associated with your first array, you might want to try keying your array by the unique id's you're generating, and set the value to the 'aa' or 'bb' or whatever. -aaron On Aug 31, 4:46 pm, "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 |
|
|||
|
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'=>'ja mes@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 |