You are here: Re: finding value in multidimensional array « PHP Language « IT news, forums, messages
Re: finding value in multidimensional array

Posted by Ian Hobson on 08/31/07 22:08

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация